Alice's Blog

Alice's Blog
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

How to Determine the Item Selected from a Property Grid Drop-down List

Posted on 2005-07-08 22:43  Alice  阅读(588)  评论(0编辑  收藏  举报
  1How to Determine the Item Selected from a Property Grid Drop-down List
from http://www.devx.com/tips/Tip/22531
  2In Visual Studio, when you select an item on a form you're building, the Property Grid appears and lets you set various properties (e.g. background color, width, height, text color, etc.) from a two-column table. The left column contains the property name, and the right column contains either a text field where you enter a value, or a drop-down list from which you can select a value. You can use the Property Grid control in your own applications, but it's not quite so clear how you can determine which value a user selects from a drop-down list that contains multiple values. This example provides the code to know which item a user selects from the drop-down list. 
  3First, inherit the class from UITypeEditor: 
  4
  5
  6public class SelEditor : 
  7   System.Drawing.Design.UITypeEditor
  8{        
  9   // this is a container for strings, which can be 
 10   // picked-out
 11   ListBox Box1 = new ListBox();
 12   IWindowsFormsEditorService edSvc;
 13   // this is a string array for drop-down list
 14   public static string[] strList;
 15
 16   public SelEditor()
 17   {
 18      Box1.BorderStyle=BorderStyle.None;
 19      // add event handler for drop-down box when item 
 20      // will be selected
 21      Box1.Click+=new EventHandler(Box1_Click);
 22   }

 23
 24   public override 
 25      System.Drawing.Design.UITypeEditorEditStyle.  
 26      GetEditStyle (System.ComponentModel.
 27         ITypeDescriptorContext context)
 28      {
 29         return UITypeEditorEditStyle.DropDown;
 30      }

 31
 32   // Displays the UI for value selection.
 33   public override object EditValue
 34      (System.ComponentModel.ITypeDescriptorContext 
 35      context, System.IServiceProvider provider, 
 36      object value)
 37   {
 38      Box1.Items.Clear();
 39      Box1.Items.AddRange(strList);
 40      Box1.Height=Box1.PreferredHeight;
 41      // Uses the IWindowsFormsEditorService to 
 42      // display a drop-down UI in the Properties 
 43      // window.
 44      edSvc =               
 45         (IWindowsFormsEditorService)provider.
 46         GetService(typeof
 47         (IWindowsFormsEditorService));
 48      if( edSvc != null )
 49      {
 50         edSvc.DropDownControl( Box1 );
 51         return Box1.SelectedItem;
 52
 53      }

 54      return value;
 55   }

 56
 57   private void Box1_Click(object sender, EventArgs e)
 58   {
 59      edSvc.CloseDropDown();
 60   }

 61}

 62
 63
 64Second, describe a property in the class, to be displayed in the property grid: 
 65
 66public class Class1
 67{
 68   // These are string arrays for different drop-down 
 69   // lists.
 70   string[] Str1= {"AAA","BBB","CCC","DDDD"};
 71   string[] Str2= {"WW","EEE"};
 72
 73   string s1,s2;
 74
 75   public Class1()
 76   {
 77      //
 78      // TODO: Add constructor logic here
 79      //
 80   }

 81
 82
 83   [EditorAttribute(typeof(SelEditor), 
 84   typeof(System.Drawing.Design.UITypeEditor))]
 85   public string STR_1
 86   {
 87      get{SelEditor.strList=Str1; return s1;}
 88      set{s1=value;}
 89   }

 90      
 91   [EditorAttribute(typeof(SelEditor), 
 92   typeof(System.Drawing.Design.UITypeEditor))]
 93   public string STR_2
 94   {
 95      get{SelEditor.strList=Str2; return s2;}
 96      set{s2=value;}
 97   }

 98}

 99
100