如何设置Winforms自定义控件中Colors的默认值?

发布于 2024-08-06 23:39:03 字数 622 浏览 4 评论 0原文

我使用以下方法获得了正确显示的值:

    [DefaultValue ( typeof ( Color ), "255, 0, 0" )]
    public Color LineColor
    {
        get { return lineColor; }
        set { lineColor = value; Invalidate ( ); }
    }

但是在重新加载项目后,使用了控件,该值设置为白色,我可以调用重置以再次返回到红色,但我不明白这个问题。

除非我手动更改默认值,否则您应该如何设置默认值并确保保留它?

实际上我也在这样做,它将 Back 和 ForeColor 设置为这些值,并且 VS 属性编辑器显示它们,就像它们从默认值更改一样。

这是错误的吗?

    public CoolGroupBox ( )
    {
        InitializeComponent ( );
        base.BackColor = Color.FromArgb ( 5, 5, 5 );
        base.ForeColor = Color.FromArgb ( 0, 0, 0 );
    }

I got the value to show up correctly using:

    [DefaultValue ( typeof ( Color ), "255, 0, 0" )]
    public Color LineColor
    {
        get { return lineColor; }
        set { lineColor = value; Invalidate ( ); }
    }

But after I reload the project the control is used, this value is set to White, which I can invoke Reset to get back to Red again, but I don't understand the problem.

How are you supposed to set the default value and make sure it's preserved unless I change the value manually from the default?

Actually I am also doing this, which sets Back and ForeColor to these values and the VS property editor shows them as if they are changed from the default value.

Is this wrong?

    public CoolGroupBox ( )
    {
        InitializeComponent ( );
        base.BackColor = Color.FromArgb ( 5, 5, 5 );
        base.ForeColor = Color.FromArgb ( 0, 0, 0 );
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

幸福%小乖 2024-08-13 23:39:03

诀窍是使用颜色的十六进制代码:

    [DefaultValue(typeof(Color), "0xFF0000")]
    public Color LineColor
    {
            get { return lineColor; }
            set { lineColor = value; Invalidate ( ); }
    }

我认为您也可以使用“255,0,0”,但我不确定并且通常使用指定颜色或十六进制代码。

The trick is to use the Hex code of the color:

    [DefaultValue(typeof(Color), "0xFF0000")]
    public Color LineColor
    {
            get { return lineColor; }
            set { lineColor = value; Invalidate ( ); }
    }

I think you can also use "255, 0, 0" but am not sure and have normally used either the named colors or the hex code.

水波映月 2024-08-13 23:39:03

将私有成员变量设置为您想要的默认颜色怎么样?

private Color lineColor = Color.Red;

public Color LineColor
{
        get { return lineColor; }
        set { lineColor = value; Invalidate ( ); }
}

如果您希望保留它,只需取出 set 访问器即可。

编辑

我明白了,您希望设计器中的属性列表显示默认颜色。

您必须重写基本控件的 BackColor 属性,为您的 new 属性添加新的 DefaultValueAttribute,然后在构造函数或 InitializeComponent() 方法(在设计器中)中实际设置默认颜色。 cs 文件),这可能更好,因为这是为设计者准备的。

public partial class RedBackgroundControl : UserControl
{
    public RedBackgroundControl()
    {
        InitializeComponent();
        base.BackColor = Color.Red;
    }

    [DefaultValue(typeof(Color), "Red")]
    new public Color BackColor
    {
        get
        {
            return base.BackColor;
        }
        set
        {
            base.BackColor = value;
        }
    }
}

What about just setting the private member variable to the default color you want?

private Color lineColor = Color.Red;

public Color LineColor
{
        get { return lineColor; }
        set { lineColor = value; Invalidate ( ); }
}

If you want it preserved, just take out the set accessor.

Edit

I see, you want the property list in the designer to show the default color.

You have to override the BackColor property of the base control, add a new DefaultValueAttribute for your new property, and then actually set the default color in the constructor or in the InitializeComponent() method (in the designer.cs file), which is probably better since this is for the designer.

public partial class RedBackgroundControl : UserControl
{
    public RedBackgroundControl()
    {
        InitializeComponent();
        base.BackColor = Color.Red;
    }

    [DefaultValue(typeof(Color), "Red")]
    new public Color BackColor
    {
        get
        {
            return base.BackColor;
        }
        set
        {
            base.BackColor = value;
        }
    }
}
瞎闹 2024-08-13 23:39:03

[DefaultValue(...)] 属性是对设计者和代码生成器的提示。它不是给编译器的指令。

有关详细信息,请参阅此知识库文章

The [DefaultValue(...)] attribute is a hint to designers and code generators. It is NOT an instruction to the compiler.

More info in this KB article.

尤怨 2024-08-13 23:39:03

我没有运气使用具有 Color 类型或 Font 类型的属性的 DefaultValue 属性,但我确实成功地使用了所描述的这些方法在 msdn 文档中:

“使用 ShouldSerialize 和 Reset 方法定义默认值”
http://msdn.microsoft.com/en- us/library/53b8022e(v=vs.90).aspx

我分别使用 Color.Emptynull 作为我的私有支持字段的值并让公共财产总是返回一些有用的东西。

I didn't have any luck using the DefaultValue attribute with properties of type Color or of type Font, but I did succeed with these methods described in the msdn documentation:

"Defining Default Values with the ShouldSerialize and Reset Methods"
http://msdn.microsoft.com/en-us/library/53b8022e(v=vs.90).aspx

I used Color.Empty and null, respectively, as the values for my private backing fields and had the public properties always return something useful.

满身野味 2024-08-13 23:39:03

您可以在 InitializeComponent 方法中的 component.designer.cs 中设置特定属性:

  private void InitializeComponent()
    {
        components = new System.ComponentModel.Container();
        this.LineColor= System.Drawing.Color.FromArgb(255, 0, 0);
    }

重新构建项目,所有内容也应该显示在

You can set specific Attribute in the component.designer.cs in the Initializecomponent Method:

  private void InitializeComponent()
    {
        components = new System.ComponentModel.Container();
        this.LineColor= System.Drawing.Color.FromArgb(255, 0, 0);
    }

Rebuild the project and everything should also show up in the

抚你发端 2024-08-13 23:39:03

我使用了这段代码,它运行得很好

Private _BackColorSelect As Color = Color.FromArgb(214, 234, 248)

<DefaultValue(GetType(Color), "214, 234, 248")>
Public Property BackColorSelect As Color
    Get
        Return _BackColorSelect
    End Get
    Set(value As Color)
        _BackColorSelect = value
    End Set
End Property

I used this code and it worked perfectly

Private _BackColorSelect As Color = Color.FromArgb(214, 234, 248)

<DefaultValue(GetType(Color), "214, 234, 248")>
Public Property BackColorSelect As Color
    Get
        Return _BackColorSelect
    End Get
    Set(value As Color)
        _BackColorSelect = value
    End Set
End Property
几味少女 2024-08-13 23:39:03

There is quite an article about defaultvalue property initialization on CodeProject

雅心素梦 2024-08-13 23:39:03

在 DependencyProperty 中使用 UIPropertyMetadata,

例如:

public static DependencyProperty TrueColorProperty = DependencyProperty.Register(
        "TrueColor", typeof (Color), typeof (LedControl), new UIPropertyMetadata(Colors.Red));

In DependencyProperty use a UIPropertyMetadata

like:

public static DependencyProperty TrueColorProperty = DependencyProperty.Register(
        "TrueColor", typeof (Color), typeof (LedControl), new UIPropertyMetadata(Colors.Red));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文