温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中怎么利用WinForm控件实现一个下拉式属性编辑器

发布时间:2021-07-19 15:50:07 来源:亿速云 阅读:206 作者:Leah 栏目:编程语言

这篇文章将为大家详细讲解有关C#中怎么利用WinForm控件实现一个下拉式属性编辑器,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

首先我们要创建一个用于编辑属性的控件,在本系列文章的开始,我们介绍了自定义控件有三种类型:复合控件,扩展控件,自定义控件。在本例中我们制作一个复合控件(Compsite control),复合控件的C# WinForm控件开发比较简单,不在本系列文章的讲解范围。

我简单做个介绍,在Solution 浏览器里右键点击CustomControlSample工程选择Add->User Control…,输入文件名ScopeEditorControl.cs。我们做的这个复合控件上一篇文章介绍的模态对话框所包含子控件基本一样,除了用于确认和取消的按钮,如下图:

C#中怎么利用WinForm控件实现一个下拉式属性编辑器
C# WinForm控件开发下拉式属性编辑器

由于我们取消了用于确认和取消的按钮,并且是一个下拉的编辑器控件,在出现下面三种情况的时候下拉的编辑器控件会关闭:用户敲了回车,用户敲了ESC键,用户点击了编辑器以外的地方。当下拉编辑器控件关闭的时候我们就需要更新属性的值。下边是这个控件的代码:

  1. using System;  

  2. using System.Collections.Generic;  

  3. using System.ComponentModel;  

  4. using System.Drawing;  

  5. using System.Data;  

  6. using System.Text;  

  7. using System.Windows.Forms;  

  8. namespace CustomControlSample  

  9. {  

  10.     public partial class ScopeEditorControl : UserControl  

  11.     {  

  12.         private Scope _oldScope;  

  13.         private Scope _newScope;  

  14.         private Boolean canceling;  

  15.         public ScopeEditorControl(Scope scope)  

  16.         {  

  17.             _oldScope = scope;  

  18.             _newScope = scope;  

  19.             InitializeComponent();  

  20.         }  

  21.         public Scope Scope  

  22.         {  

  23.             get 

  24.             {  

  25.                 return _newScope;  

  26.             }  

  27.         }  

  28.         private void textBox1_Validating(object sender, CancelEventArgs e)  

  29.         {  

  30.             try 

  31.             {  

  32.                 Int32.Parse(textBox1.Text);  

  33.             }  

  34.             catch (FormatException)  

  35.             {  

  36.                 e.Cancel = true;  

  37.                 MessageBox.Show("无效的值""验证错误",
    MessageBoxButtons.OK, MessageBoxIcon.Error);  

  38.             }  

  39.         }  

  40.         private void textBox2_Validating(object sender, CancelEventArgs e)  

  41.         {  

  42.             try 

  43.             {  

  44.                 Int32.Parse(textBox2.Text);  

  45.             }  

  46.             catch (FormatException)  

  47.             {  

  48.                 e.Cancel = true;  

  49.                 MessageBox.Show("无效的值""验证错误",
    MessageBoxButtons.OK, MessageBoxIcon.Error);  

  50.             }  

  51.         }  

  52.        protected override bool ProcessDialogKey(Keys keyData)  

  53.         {  

  54.             if (keyData == Keys.Escape)  

  55.             {  

  56.                 _oldScope = _newScope;  

  57.                 canceling = true;  

  58.             }  

  59.             return base.ProcessDialogKey(keyData);  

  60.         }  

  61.         private void ScopeEditorControl_Leave(object sender, EventArgs e)  

  62.         {  

  63.             if (!canceling)  

  64.             {  

  65.                 _newScope.Max = Convert.ToInt32(textBox1.Text);  

  66.                 _newScope.Min = Convert.ToInt32(textBox2.Text);  

  67.             }  

  68.         }  

  69.         private void ScopeEditorControl_Load(object sender, EventArgs e)  

  70.         {  

  71.             textBox1.Text = _oldScope.Max.ToString();  

  72.             textBox2.Text = _oldScope.Min.ToString();  

  73.         }  

  74.     }  

和模态对话框编辑器一样,C# WinForm控件开发环境并不会直接调用我们的编辑器控件,而是用过UITypeEditor类的派生来实现编辑器的调用,所以我们必须实现一个下拉式编辑器。代码如下:

  1. using System;  

  2. using System.ComponentModel;  

  3. using System.Drawing.Design;  

  4. using System.Windows.Forms.Design;  

  5. using System.Windows.Forms;  

  6. namespace CustomControlSample  

  7. {  

  8.     public class ScopeDropDownEditor : UITypeEditor  

  9.     {  

  10.         public override UITypeEditorEditStyle GetEditStyle
    (ITypeDescriptorContext context)  

  11.         {  

  12.             if (context != null && context.Instance != null)  

  13.             {  

  14.                 return UITypeEditorEditStyle.DropDown;  

  15.             }  

  16.             return base.GetEditStyle(context);  

  17.         }  

  18.         public override object EditValue(ITypeDescriptorContext context, 
    IServiceProvider provider, object value)  

  19.         {  

  20.             IWindowsFormsEditorService editorService = null;  

  21.             if (context !=
    null && context.Instance != null && provider != null)  

  22.             {  

  23.                 editorService = 
    (IWindowsFormsEditorService)provider.GetService
    (typeof(IWindowsFormsEditorService));  

  24.                 if (editorService != null)  

  25.                 {  

  26.                     MyListControl control = 
    (MyListControl)context.Instance;  

  27.                     ScopeEditorControl editorControl = 
    new ScopeEditorControl(control.Scope);  

  28.                     editorService.DropDownControl(editorControl);  

  29.                     value = editorControl.Scope;  

  30.                     return value;  

  31.                 }  

  32.             }  

  33.             return value;  

  34.         }  

  35.     }  

  36. }  

  37.  

看过上一篇文章的朋友应该对这段代码很熟悉,是的,这两个编辑器的代码只有几行不同之处,在GetEditStyle方法中,我们返回的是UITypeEditorEditStyle.DropDown,而不是UITypeEditorEditStyle.Modal,表明我们的编辑器是一个下拉式的编辑器。

在EditValue中的不同之处是,我们使用DropDownControl方法来显示编辑器。编辑器制作完毕,我们把Scope以前的编辑器替换成下拉式编辑器,如下:

[Browsable(true)]          [Editor(typeof(ScopeDropDownEditor), typeof(UITypeEditor))]          public Scope Scope          {              get             {                  return _scope;              }              set             {                  _scope = value;              }          }

现在build CustomControlSample工程,然后切换到测试工程查看Scope属性。当我们点击属性的值,在属性值的后边出现了一个按钮:

C#中怎么利用WinForm控件实现一个下拉式属性编辑器
C# WinForm控件开发下拉式属性编辑器

当点击这个按钮的时候,下拉的属性编辑器出现了:

好了,C# WinForm控件开发的下拉式属性编辑器的编辑到这里就讲完了。

C#中怎么利用WinForm控件实现一个下拉式属性编辑器 
C# WinForm控件开发下拉式属性编辑器

关于C#中怎么利用WinForm控件实现一个下拉式属性编辑器就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI