下拉框在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

相关文章

DNS查询的利器! linux的dig命令基本用法详解

《DNS查询的利器!linux的dig命令基本用法详解》dig命令可以查询各种类型DNS记录信息,下面我们将通过实际示例和dig命令常用参数来详细说明如何使用dig实用程序... dig(Domain Information Groper)是一款功能强大的 linux 命令行实用程序,通过查询名称服务器并输

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

javaSE类和对象进阶用法举例详解

《javaSE类和对象进阶用法举例详解》JavaSE的面向对象编程是软件开发中的基石,它通过类和对象的概念,实现了代码的模块化、可复用性和灵活性,:本文主要介绍javaSE类和对象进阶用法的相关资... 目录前言一、封装1.访问限定符2.包2.1包的概念2.2导入包2.3自定义包2.4常见的包二、stati

C语言中%zu的用法解读

《C语言中%zu的用法解读》size_t是无符号整数类型,用于表示对象大小或内存操作结果,%zu是C99标准中专为size_t设计的printf占位符,避免因类型不匹配导致错误,使用%u或%d可能引发... 目录size_t 类型与 %zu 占位符%zu 的用途替代占位符的风险兼容性说明其他相关占位符验证示

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

全面解析Golang 中的 Gorilla CORS 中间件正确用法

《全面解析Golang中的GorillaCORS中间件正确用法》Golang中使用gorilla/mux路由器配合rs/cors中间件库可以优雅地解决这个问题,然而,很多人刚开始使用时会遇到配... 目录如何让 golang 中的 Gorilla CORS 中间件正确工作一、基础依赖二、错误用法(很多人一开

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

Java Spring的依赖注入理解及@Autowired用法示例详解

《JavaSpring的依赖注入理解及@Autowired用法示例详解》文章介绍了Spring依赖注入(DI)的概念、三种实现方式(构造器、Setter、字段注入),区分了@Autowired(注入... 目录一、什么是依赖注入(DI)?1. 定义2. 举个例子二、依赖注入的几种方式1. 构造器注入(Con

详解MySQL中JSON数据类型用法及与传统JSON字符串对比

《详解MySQL中JSON数据类型用法及与传统JSON字符串对比》MySQL从5.7版本开始引入了JSON数据类型,专门用于存储JSON格式的数据,本文将为大家简单介绍一下MySQL中JSON数据类型... 目录前言基本用法jsON数据类型 vs 传统JSON字符串1. 存储方式2. 查询方式对比3. 索引

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1