本文主要是介绍下拉框在PropertyGrid的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本例子在项目中已试验通过。
直接看代码例子:
- #region bill of material drop-down list
- public class DropDownListBillConverter : System.Drawing.Design.UITypeEditor
- {
- public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
- {
- return System.Drawing.Design.UITypeEditorEditStyle.DropDown;
- }
- public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
- {
- System.Windows.Forms.Design.IWindowsFormsEditorService iws = (System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));
- if (iws != null)
- {
- DropDownListBillControll dlc = new DropDownListBillControll(iws,value);
- iws.DropDownControl(dlc);
- return (Bill)dlc.SelectedItem;
- }
- return value;
- }
- }
- public class DropDownListBillControll : System.Windows.Forms.ListBox
- {
- public DropDownListBillControll(System.Windows.Forms.Design.IWindowsFormsEditorService iws,object value)
- {
- //inite dropdownlist value
- DataSet ds = FormDesignerDataAccess.BomGroups;
- if (ds != null && ds.Tables[0]!=null && ds.Tables[0].Rows.Count>0)
- {
- foreach (DataRow dr in ds.Tables[0].Rows)
- {
- Bill b = new Bill();
- b.GroupId = dr["GroupId"].ToString();
- b.GroupName = dr["GroupName"].ToString();
- this.Items.Add(b);
- }
- }
- if (value != null)
- {
- string selectevalue = ((Bill)value).GroupId;
- for (int i = 0; i < this.Items.Count; i++)
- {
- if (((Bill)this.Items[i]).GroupId == selectevalue)
- {
- this.SelectedIndex = i;
- break;
- }
- }
- }
- this._iws = iws;
- this.Visible = true;
- this.BorderStyle = System.Windows.Forms.BorderStyle.None;
- this.SelectedValueChanged += new EventHandler(cb_SelectedValueChanged);
- this.DisplayMember = "GroupName";
- this.ValueMember = "GroupId";
- }
- private System.Windows.Forms.Design.IWindowsFormsEditorService _iws;
- private void cb_SelectedValueChanged(object sender, EventArgs e)
- {
- if (this.SelectedItem != null)
- {
- //this.select = (Bill)this.SelectedItem;
- this._iws.CloseDropDown();
- }
- }
- }
- public class Bill
- {
- public string GroupId{get;set;}
- public string GroupName{get;set;}
- public override string ToString()
- {
- return GroupName;
- }
- }
- #endregion
用法如下:
- public class BillOfMaterialsQuestion : IQuestion
- {
- /// <summary>
- /// If this question is required.
- /// </summary>
- [DescriptionAttribute("If this question is required.")]
- public bool Required { get; set; }
- private Bill m_Part;
- [ReadOnlyAttribute(false),
- BrowsableAttribute(true),
- EditorAttribute(typeof(DropDownListBillConverter), typeof(System.Drawing.Design.UITypeEditor)),
- DescriptionAttribute("Parts list")
- ]
- public Bill Part
- {
- get { return m_Part; }
- set { m_Part = value; }
- }
- public BillOfMaterialsQuestion()
- {
- }
- }
这篇关于下拉框在PropertyGrid的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!