下拉框在PropertyGrid的用法

本例子在项目中已试验通过。

直接看代码例子:

  1. #region bill of material drop-down list
  2.     public class DropDownListBillConverter : System.Drawing.Design.UITypeEditor
  3.     {
  4.         public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  5.         {
  6.             return System.Drawing.Design.UITypeEditorEditStyle.DropDown;
  7.         }
  8.         
  9.         public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
  10.         {
  11.             System.Windows.Forms.Design.IWindowsFormsEditorService iws = (System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));
  12.             if (iws != null)
  13.             {
  14.                 DropDownListBillControll dlc = new DropDownListBillControll(iws,value);
  15.                 iws.DropDownControl(dlc);
  16.                 return (Bill)dlc.SelectedItem;
  17.             }
  18.             return value;
  19.         }
  20.     }
  21.     public class DropDownListBillControll : System.Windows.Forms.ListBox
  22.     {
  23.         public DropDownListBillControll(System.Windows.Forms.Design.IWindowsFormsEditorService iws,object value)
  24.         {
  25.             //inite dropdownlist value
  26.             DataSet ds = FormDesignerDataAccess.BomGroups;
  27.             
  28.             if (ds != null && ds.Tables[0]!=null && ds.Tables[0].Rows.Count>0)
  29.             {
  30.                 foreach (DataRow dr in ds.Tables[0].Rows)
  31.                 {
  32.                     Bill b = new Bill();
  33.                     b.GroupId = dr["GroupId"].ToString();
  34.                     b.GroupName = dr["GroupName"].ToString();
  35.                     this.Items.Add(b);
  36.                 }                
  37.             }
  38.             if (value != null)
  39.             {
  40.                 string selectevalue = ((Bill)value).GroupId;
  41.                 for (int i = 0; i < this.Items.Count; i++)
  42.                 {
  43.                     if (((Bill)this.Items[i]).GroupId == selectevalue)
  44.                     {
  45.                         this.SelectedIndex = i;
  46.                         break;
  47.                     }
  48.                 }
  49.             }
  50.             this._iws = iws;
  51.             this.Visible = true;
  52.             this.BorderStyle = System.Windows.Forms.BorderStyle.None;
  53.             this.SelectedValueChanged += new EventHandler(cb_SelectedValueChanged);
  54.             this.DisplayMember = "GroupName";
  55.             this.ValueMember = "GroupId";
  56.         }
  57.         
  58.         private System.Windows.Forms.Design.IWindowsFormsEditorService _iws;
  59.         private void cb_SelectedValueChanged(object sender, EventArgs e)
  60.         {
  61.             if (this.SelectedItem != null)
  62.             {
  63.                 //this.select = (Bill)this.SelectedItem;
  64.                 this._iws.CloseDropDown();
  65.             }
  66.         }
  67.     }
  68.     public class Bill
  69.     {
  70.         public string GroupId{get;set;}
  71.         public string GroupName{get;set;}
  72.         public override string ToString()
  73.         {
  74.             return GroupName; 
  75.         }
  76.     }
  77.     #endregion

用法如下:

  1. public class BillOfMaterialsQuestion : IQuestion
  2.     {
  3.         /// <summary>
  4.         /// If this question is required.
  5.         /// </summary>
  6.         [DescriptionAttribute("If this question is required.")]
  7.         public bool Required { getset; }
  8.         private Bill m_Part;
  9.         [ReadOnlyAttribute(false),
  10.         BrowsableAttribute(true),
  11.         EditorAttribute(typeof(DropDownListBillConverter), typeof(System.Drawing.Design.UITypeEditor)),
  12.         DescriptionAttribute("Parts list")
  13.         ]
  14.         public Bill Part
  15.         {
  16.             get { return m_Part; }
  17.             set { m_Part = value; }
  18.         }
  19.         public BillOfMaterialsQuestion()
  20.         {
  21.              
  22.         }
  23.     }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值