Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';似乎无法更改WinC窗体中对象的属性#_C#_Windows_Winforms_Visual Studio - Fatal编程技术网

C# Can';似乎无法更改WinC窗体中对象的属性#

C# Can';似乎无法更改WinC窗体中对象的属性#,c#,windows,winforms,visual-studio,C#,Windows,Winforms,Visual Studio,这是一个令人困惑的场景。基本上,当我为存储按钮颜色的私有变量使用setter时,我试图设置按钮的颜色 首先,我有一个单独的定制窗口。当我更改按钮颜色时,我也想更改此窗口中的每个按钮。我将它存储在主窗体类中的静态变量中 public static frm_custom customizer; 这是所讨论的变量的setter private Color _buttonColor; public Color buttonColor { get { return this._buttonCol

这是一个令人困惑的场景。基本上,当我为存储按钮颜色的私有变量使用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);
        }
    }
}
private Color\u按钮颜色;
公共颜色按钮颜色
{
获取{返回此。_buttonColor;}
设置
{
this.btn_input.BackColor=按钮颜色;
这个._buttonColor=buttonColor;
if(Application.OpenForms.OfType().Count()==1)
{
customizer.setButtonColor(按钮颜色);
}
}
}
奇怪的是,它根本不影响颜色。我做错什么了吗

我做错什么了吗

对。您的setter正在获取现有属性值:

this.btn_input.BackColor = buttonColor;
this._buttonColor = buttonColor;
您打算使用
value
,这是setter的隐式参数名称:

this.btn_input.BackColor = value;
this._buttonColor = value;
(您的
if
块也是如此,但很难说它是如何工作的,因为它目前不是有效的C#。)

作为补充说明,我强烈建议您开始遵循.NET命名约定,其中包括属性的大写字母-因此
ButtonColor
而不是
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

您能显示实际的代码吗?上面的代码无法编译。你不能在属性中的
set
块后放置一个
if
语句。他只是有一个不好的大括号。@Trey:这会阻止它编译。但这并不是代码的全部问题。是和否,他在设置属性backer之后设置颜色,所以它什么也不做,加上设置颜色的代码不在集合的大括号内……当我在这里发布时,大括号是一个错误。修正了。你能显示实际代码吗?上面的代码无法编译。你不能在属性中的
set
块后放置一个
if
语句。他只是有一个不好的大括号。@Trey:这会阻止它编译。但这并不是代码的全部问题。是和否,他在设置属性backer之后设置颜色,所以它什么也不做,加上设置颜色的代码不在集合的大括号内……当我在这里发布时,大括号是一个错误。修复了它。@RamaRaunt如果这个答案解决了您的问题,请接受它。@RamaRaunt如果这个答案解决了您的问题,请接受它。