简体   繁体   中英

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:

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

I don't remember whether the property grid is aware of [Flags] enums; if not, you'll need to write a 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] . That should be easy to remember for next time...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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