似乎无法在winforms C#中更改对象的属性

Ram*_*unt 1 c# windows visual-studio winforms

这有点令人困惑.基本上,当我使用setter作为我存储其颜色的私有变量时,我正在尝试设置按钮的颜色.

首先,我有一个单独的窗口来定制东西.当我更改按钮颜色时,我也想更改此窗口中的每个按钮.我将它存储在我的主窗体类中的静态变量中.

public static frm_custom customizer;
Run Code Online (Sandbox Code Playgroud)

这是有问题的变量的setter.

private Color _buttonColor;
public Color buttonColor
{
    get { return this._buttonColor; }
    set
    {
        this.btn_input.BackColor = buttonColor;
        this._buttonColor = buttonColor;
        if (Application.OpenForms.OfType<frm_custom>().Count() == 1)
        {
            customizer.setButtonColor(buttonColor);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,它根本不会影响颜色.我做错什么了吗?

Jon*_*eet 7

我做错什么了吗?

是.你的setter只是获取现有的属性值:

this.btn_input.BackColor = buttonColor;
this._buttonColor = buttonColor;
Run Code Online (Sandbox Code Playgroud)

您打算使用value,这是setter的隐式参数名称:

this.btn_input.BackColor = value;
this._buttonColor = value;
Run Code Online (Sandbox Code Playgroud)

(同样适用于您的if块,但很难说这是什么意思,因为它目前无效C#.)

作为旁注,我强烈建议您开始遵循.NET命名约定,其中包括属性的大写字母 - ButtonColor而不是buttonColor.