这有点让人困惑。基本上,当我使用setter来存储一个私有变量的颜色时,我试图设置一个按钮的颜色。
首先,我有一个单独的窗口来定制东西。当我改变按钮的颜色时,我也想改变这个窗口中的每个按钮。我把它存储在主窗体类的静态变量中。
public static frm_custom customizer;
这是有关变量的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);
}
}
}
奇怪的是,它一点也不影响颜色。我做错什么了吗?
最佳答案
我做错什么了吗?
对。setter正在获取现有属性值:
this.btn_input.BackColor = buttonColor;
this._buttonColor = buttonColor;
您打算使用
value
,这是setter的隐式参数名:this.btn_input.BackColor = value;
this._buttonColor = value;
(对于您的
if
块也是一样,但是很难说明它是如何工作的,因为它目前不是有效的c。)作为补充说明,我强烈建议您开始遵循.NET命名约定,其中包括属性的大写字母-所以
ButtonColor
而不是buttonColor
。关于c# - 似乎无法更改Winforms C#中对象的属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44032070/