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

相关文章

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

Java中HashMap的用法详细介绍

《Java中HashMap的用法详细介绍》JavaHashMap是一种高效的数据结构,用于存储键值对,它是基于哈希表实现的,提供快速的插入、删除和查找操作,:本文主要介绍Java中HashMap... 目录一.HashMap1.基本概念2.底层数据结构:3.HashCode和equals方法为什么重写Has

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

Python库 Django 的简介、安装、用法入门教程

《Python库Django的简介、安装、用法入门教程》Django是Python最流行的Web框架之一,它帮助开发者快速、高效地构建功能强大的Web应用程序,接下来我们将从简介、安装到用法详解,... 目录一、Django 简介 二、Django 的安装教程 1. 创建虚拟环境2. 安装Django三、创

python中update()函数的用法和一些例子

《python中update()函数的用法和一些例子》update()方法是字典对象的方法,用于将一个字典中的键值对更新到另一个字典中,:本文主要介绍python中update()函数的用法和一些... 目录前言用法注意事项示例示例 1: 使用另一个字典来更新示例 2: 使用可迭代对象来更新示例 3: 使用

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除