enum Property

hi ppl, i have a custom control, and i need a property that have multiple options, its a property that sets the status of the control, and i need to have the next properties:

Active
Inactive
Off

and i thought i could get this with an enum , but i dont get a clue of how to make a property that handles more than 2 options...

am i right can some 1 give me some code pls

mig16


Answer this question

enum Property

  • amat2to0

    You need to create a designer. . . its rather easy. . .
    Play along, ok
    1. Create a new Blank Solution - FlaggedProperty
    2. In the solution explorer, right click the solution a new Windows Control Library Project - FlagProperty.Designer
    3. Rename UserControl1.cs to FlagPropertyDesignerPanel.cs
    4. Drop a CheckListBox on the panel. click the smart tag and select "Dock to Container" and rename to "checkedValues"
    5. Add the following code:
      Listing 1 - FlagPropertyDesignerPanel.cs
      using System;
      using
      System.ComponentModel;
      using
      System.Windows.Forms;

      namespace
      FlagProperty.Designer
      {
      public partial class FlagPropertyDesignerPanel : UserControl
      {
      Type _flagType;

      public FlagPropertyDesignerPanel(Type flagType)
      {
      InitializeComponent();
      _flagType = flagType;
      checkedValues.Items.Clear();
      checkedValues.CheckOnClick =
      true;
      foreach(object flag in Enum.GetValues(_flagType))
      if (Convert.ToUInt64(flag) != 0)
      checkedValues.Items.Add(flag);
      }

      public
      object Value
      {
      get
      {
      UInt64 value = 0;
      foreach (object flag in checkedValues.CheckedItems)
      value |=
      Convert.ToUInt64(flag);
      return Enum.ToObject(_flagType, value);
      }
      set
      {
      Array flags = Array.CreateInstance(typeof(Object),
      checkedValues.Items.Count);
      checkedValues.Items.CopyTo((
      object[])flags, 0);
      foreach (object flag in flags)
      {
      checkedValues.SetItemChecked(
      checkedValues.Items.IndexOf(flag),
      (
      Convert.ToUInt64(flag) & Convert.ToUInt64(value)) != 0);
      }
      }
      }
      }
      }

    6. Add a new class - FlagDesigner with the following code:
      Listing 2 - FlagPropertyDesigner.cs
      using System;
      using
      System.ComponentModel;
      using
      System.Drawing.Design;
      using
      System.Windows.Forms.Design;
      using
      System.Security.Permissions;

      namespace
      FlagProperty.Designer
      {
      public class FlagPropertyDesigner : System.Drawing.Design.UITypeEditor
      {

      [
      PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
      public override UITypeEditorEditStyle GetEditStyle(
      ITypeDescriptorContext context)
      {
      return UITypeEditorEditStyle.DropDown;
      }

      [
      PermissionSet( SecurityAction.Demand, Name = "FullTrust")]
      public override object EditValue( ITypeDescriptorContext context,
      IServiceProvider provider, object value)
      {
      IWindowsFormsEditorService edSvc =
      (
      IWindowsFormsEditorService) provider.GetService(
      typeof(IWindowsFormsEditorService));
      if (edSvc == null) return value;
      FlagPropertyDesignerPanel panel =
      new FlagPropertyDesignerPanel(
      context.PropertyDescriptor.PropertyType);
      panel.Value = value;
      edSvc.DropDownControl(panel);
      return Enum.ToObject(
      context.PropertyDescriptor.PropertyType, panel.Value);
      }

      public
      override bool GetPaintValueSupported(
      ITypeDescriptorContext context)
      {
      return false;
      }
      }
      }
    7. Build the solution
    8. Now to use it. Create a new Class Library project - FlagProperty. Add a project reference to "FlagProperty.Designer" and .Net references to System.Drawing and System.Drawing.Design
    9. Delete Class1.cs. and add a new component "FlagPropertyComponent.cs" and put the following code in it:
      Listing 3 - FlagDesigner.cs
      using System;
      using
      System.ComponentModel;
      using
      FlagProperty.Designer;

      namespace
      FlagProperty
      {
      public partial class FlagPropertyComponent : Component
      {
      FlagValue1 _value1;
      FlagValue2 _value2;

      public FlagPropertyComponent()
      {
      InitializeComponent();
      }

      public
      FlagPropertyComponent(IContainer container)
      {
      container.Add(
      this);
      InitializeComponent();
      }

      [
      EditorAttribute(typeof(FlagPropertyDesigner),
      typeof(System.Drawing.Design.UITypeEditor))]
      public FlagValue1 Value1
      {
      get
      {
      return _value1;
      }
      set
      {
      _value1 =
      value;
      }
      }

      [
      EditorAttribute(typeof(FlagPropertyDesigner),
      typeof(System.Drawing.Design.UITypeEditor))]
      public FlagValue2 Value2
      {
      get
      {
      return _value2;
      }
      set
      {
      _value2 =
      value;
      }
      }
      }

      [
      Flags, Serializable]
      public enum FlagValue1
      {
      none,
      flag1 = 0x01,
      flag2 = 0x02,
      flag3 = 0x04,
      flag4 = 0x08,
      flag5 = 0x10,
      }

      [
      Flags, Serializable]
      public enum FlagValue2
      {
      none,
      flag1 = 0x01,
      flag2 = 0x02,
      flag3 = 0x04,
      flag4 = 0x08,
      flag5 = 0x10,
      }
      }
    10. Build the solution
    11. Now to Test it. Add a new Windows Forms Application - TestFlags. FlagPropertyComponent should be available in the ToolBox if the project is still in the explorer. Drop one on the form and check the boxes for your required values in the property editor. Done!!!

    Complete source here



  • Madhu K Nair

    I don't understand your problem with "a property handles more that 2 options" but here's some code that might help:

    public class UserControl1 : UserControl
    {
    private ControlStatus status;

    public ControlStatus Status
    {
    get
    {
    return status;
    }
    set
    {
    status = value;
    Invalidate();
    }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
    base.OnPaint(e);

    switch (status)
    {
    case ControlStatus.Active:
    e.Graphics.FillRectangle(SystemBrushes.Window, ClientRectangle);
    break;


    case ControlStatus.Inactive:
    e.Graphics.FillRectangle(SystemBrushes.Control, ClientRectangle);
    break;

    case ControlStatus.Off:
    e.Graphics.FillRectangle(SystemBrushes.ControlDark, ClientRectangle);
    break;
    }
    }
    }

    public enum ControlStatus
    {
    Active,
    Inactive,
    Off
    }


  • polytx

    heres an image of the designer in action:



  • Klimov Vova

    lol bro, hehehehe how ever ur post was usefull,,, its a good post that i will use later in my project, thank u also :)

  • SPGMuthu

    Oops. . . I missed your point. I saw "multiple options" on a UserControl . . . thought you needed to make a flag designer.

    At any rate, put that code in your "set of tools" its very handy



  • Utku

    hi,

    you can do something like this


    namespace ConsoleApplication1
    {
    enum MyEnum

    {
    Off,
    Inactive,
    Active
    }
    class Program

    {
    private static int myVar;
    public static MyEnum MyProperty
    {
    get

    {
    return (MyEnum)myVar;
    }
    set

    {
    myVar = (
    int)value;
    }
    }

    static void Main(string[] args)
    {
    MyProperty =
    MyEnum.Active;
    Console.WriteLine(myVar);
    MyProperty =
    MyEnum.Inactive;
    Console.WriteLine(MyProperty);
    }
    }
    }


    i set myvar as int but you can make it of type MyEnum too and remove the casting from Myproperty

    hope this helps



  • enum Property