简体   繁体   English

控制自定义属性

[英]Control custom properties

I'm just learning about custom controls in C# (window forms) I've created the below custom control, as you can see I have a propery called "Test" that should be set to an enum value of EnumTest - it's working find, except what I would like is for the user of the control to select more than one property so the "Test" property can be:我只是在了解 C#(窗口窗体)中的自定义控件我已经创建了下面的自定义控件,如您所见,我有一个名为“Test”的属性,它应该设置为 EnumTest 的枚举值 - 它正在工作中找到,除了我想要的是控件的用户对 select 多个属性,所以“测试”属性可以是:

Test = EnumTest.TopLeft | EnumTest.TopRight;

Is this possible - and if so, how as the drop down box in properties only allows me to select one enum in the list.这可能吗 - 如果是这样,属性中的下拉框如何只允许我 select 列表中的一个枚举。 Also if possible I need to detect that if the user set it to "None" then it would be a single choice rather than multi choice.另外,如果可能的话,我需要检测到如果用户将其设置为“无”,那么它将是单选而不是多选。

namespace WindowsFormsApplication1
{
    public partial class myControl1 : Control 
    {
        public enum EnumTest
        {
            None = 0,
            TopLeft = 1,
            TopRight = 2,
            BottomLeft = 4,
            BottomRight = 8,
            All = TopLeft | TopRight | BottomLeft | BottomRight
        }
        public UserControl1() {
            InitializeComponent();
        }

        public EnumTest Test {
            get;
            set;
        }
    }
}

Many thanks for any help on this.非常感谢您对此的任何帮助。

Add [Flags] to your enum to indicate that it accepts multiple values.[Flags]添加到您的枚举以表明它接受多个值。

I don't remember whether the property grid is aware of [Flags] enums;我不记得属性网格是否知道[Flags]枚举; if not, you'll need to write a UITypeEditor.如果没有,您需要编写一个 UITypeEditor。

Wasn't there an attribute to enum for specifying flag enums?没有用于指定标志枚举的枚举属性吗? Wait a moment, I'm looking into this...等一下,我正在研究这个...

Hey, what do you know: They called the attribute [Flags] .嘿,你知道什么:他们将属性称为[Flags] That should be easy to remember for next time...下次应该很容易记住了……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM