下拉框在PropertyGrid的用法

2024-01-16 16:08

本文主要是介绍下拉框在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.     }

 

 

这篇关于下拉框在PropertyGrid的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/613144

相关文章

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

Java序列化之serialVersionUID的用法解读

《Java序列化之serialVersionUID的用法解读》Java序列化之serialVersionUID:本文介绍了Java对象的序列化和反序列化过程,强调了serialVersionUID的作... 目录JavChina编程a序列化之serialVersionUID什么是序列化为什么要序列化serialV

python3中正则表达式处理函数用法总结

《python3中正则表达式处理函数用法总结》Python中的正则表达式是一个强大的文本处理工具,用于匹配、查找、替换等操作,在Python中正则表达式的操作主要通过内置的re模块来实现,这篇文章主要... 目录前言re.match函数re.search方法re.match 与 re.search的区别检索

MySQL 中的 JSON_CONTAIN用法示例详解

《MySQL中的JSON_CONTAIN用法示例详解》JSON_CONTAINS函数用于检查一个JSON文档中是否包含另一个JSON文档,这篇文章给大家介绍JSON_CONTAINS的用法、语法、... 目录深入了解 mysql 中的 jsON_CONTAINS1. JSON_CONTAINS 函数的概述2

JDK21对虚拟线程的几种用法实践指南

《JDK21对虚拟线程的几种用法实践指南》虚拟线程是Java中的一种轻量级线程,由JVM管理,特别适合于I/O密集型任务,:本文主要介绍JDK21对虚拟线程的几种用法,文中通过代码介绍的非常详细,... 目录一、参考官方文档二、什么是虚拟线程三、几种用法1、Thread.ofVirtual().start(

Java8 Collectors.toMap() 的两种用法

《Java8Collectors.toMap()的两种用法》Collectors.toMap():JDK8中提供,用于将Stream流转换为Map,本文给大家介绍Java8Collector... 目录一、简单介绍用法1:根据某一属性,对对象的实例或属性做映射用法2:根据某一属性,对对象集合进行去重二、Du

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

Python中的sort方法、sorted函数与lambda表达式及用法详解

《Python中的sort方法、sorted函数与lambda表达式及用法详解》文章对比了Python中list.sort()与sorted()函数的区别,指出sort()原地排序返回None,sor... 目录1. sort()方法1.1 sort()方法1.2 基本语法和参数A. reverse参数B.

vue监听属性watch的用法及使用场景详解

《vue监听属性watch的用法及使用场景详解》watch是vue中常用的监听器,它主要用于侦听数据的变化,在数据发生变化的时候执行一些操作,:本文主要介绍vue监听属性watch的用法及使用场景... 目录1. 监听属性 watch2. 常规用法3. 监听对象和route变化4. 使用场景附Watch 的

Java Instrumentation从概念到基本用法详解

《JavaInstrumentation从概念到基本用法详解》JavaInstrumentation是java.lang.instrument包提供的API,允许开发者在类被JVM加载时对其进行修改... 目录一、什么是 Java Instrumentation主要用途二、核心概念1. Java Agent