C# 如何在Winforms的自定义控件中设置颜色的默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1522199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:22:49  来源:igfitidea点击:

How to set the default value of Colors in a custom control in Winforms?

c#.netgdi+winforms

提问by Joan Venge

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.

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

Is this wrong?

这是错误的吗?

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

采纳答案by Serge Wautier

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

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

More info in this KB article.

此知识库文章中的更多信息。

回答by scottm

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.

如果你想保留它,只需取出 set 访问器。

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 newproperty, 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.

您必须覆盖基本控件的 BackColor 属性,为您的属性添加一个新的 DefaultValueAttribute ,然后在构造函数或 InitializeComponent() 方法(在designer.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;
        }
    }
}

回答by JDunkerley

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.

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

回答by marchewek

There is quite an article about defaultvalue property initialization on CodeProject

CodeProject上有一篇关于defaultvalue属性初始化的文章

回答by Michal Pl?ko

In DependencyProperty use a UIPropertyMetadata

在 DependencyProperty 中使用 UIPropertyMetadata

like:

喜欢:

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

回答by Kerran

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

我在使用DefaultValue具有 typeColor或 type属性的属性时没有任何运气Font,但我确实成功使用了 msdn 文档中描述的这些方法:

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

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

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

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

回答by Eych

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

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

  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

重建项目,一切都应该出现在