WinForm控件开发总结(九)-----为属性提下拉式属性编辑器

简介:
  在上一篇文章,我介绍了如何编写模态对话框属性编辑器,这篇文章我将介绍如何编写下拉式属性编辑器。下拉式( DropDown )属性编辑器和模态对话框属性编辑器的不同之处就是,当你点击属性值修改的时候,模态对话框编辑器是弹出一个模态对话框,而下拉式属性编辑器却是在紧贴着属性值的地方显示一个下拉的控件。不知道大家注意到了没有,这里我说的是显示一个下拉的控件,而这个控件也是需要你去开发的,接下来我还是以 Scope 属性为例,介绍一下具体的实现。
       首先我们要创建一个用于编辑属性的控件,在本系列文章的开始,我们介绍了自定义控件有三种类型:复合控件,扩展控件,自定义控件。在本例中我们制作一个复合控件( Compsite control ),复合控件的开发比较简单,不在本系列文章的讲解范围,我简单做个介绍,在 Solution  浏览器里右键点击 CustomControlSample 工程选择 Add->User Control…, 输入文件名 ScopeEditorControl.cs 。我们做的这个复合控件上一篇文章介绍的模态对话框所包含子控件基本一样,除了用于确认和取消的按钮,如下图:
      
      由于我们取消了用于确认和取消的按钮,并且是一个下拉的编辑器控件,在出现下面三种情况的时候下拉的编辑器控件会关闭:用户敲了回车,用户敲了 ESC 键,用户点击了编辑器以外的地方。当下拉编辑器控件关闭的时候我们就需要更新属性的值。下边是这个控件的代码:
      
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Drawing;
None.gif
using  System.Data;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
namespace  CustomControlSample
ExpandedBlockStart.gif
{
InBlock.gif    
public partial class ScopeEditorControl : UserControl
ExpandedSubBlockStart.gif    
{
InBlock.gif        
private Scope _oldScope;
InBlock.gif        
private Scope _newScope;
InBlock.gif        
private Boolean canceling;
InBlock.gif        
InBlock.gif        
public ScopeEditorControl(Scope scope)
ExpandedSubBlockStart.gif        
{
InBlock.gif            _oldScope 
= scope;
InBlock.gif            _newScope 
= scope;
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public Scope Scope
ExpandedSubBlockStart.gif        
{
InBlock.gif            
get
ExpandedSubBlockStart.gif            
{
InBlock.gif                
return _newScope;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void textBox1_Validating(object sender, CancelEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                Int32.Parse(textBox1.Text);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (FormatException)
ExpandedSubBlockStart.gif            
{
InBlock.gif                e.Cancel 
= true;
InBlock.gif                MessageBox.Show(
"无效的值""验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void textBox2_Validating(object sender, CancelEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                Int32.Parse(textBox2.Text);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (FormatException)
ExpandedSubBlockStart.gif            
{
InBlock.gif                e.Cancel 
= true;
InBlock.gif                MessageBox.Show(
"无效的值""验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override bool ProcessDialogKey(Keys keyData)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
if (keyData == Keys.Escape)
ExpandedSubBlockStart.gif            
{
InBlock.gif                _oldScope 
= _newScope;
InBlock.gif                canceling 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return base.ProcessDialogKey(keyData);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ScopeEditorControl_Leave(object sender, EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
if (!canceling)
ExpandedSubBlockStart.gif            
{
InBlock.gif                _newScope.Max 
= Convert.ToInt32(textBox1.Text);
InBlock.gif                _newScope.Min 
= Convert.ToInt32(textBox2.Text);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ScopeEditorControl_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            textBox1.Text 
= _oldScope.Max.ToString();
InBlock.gif            textBox2.Text 
= _oldScope.Min.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

          和模态对话框编辑器一样,开发环境并不会直接调用我们的编辑器控件,而是用过 UITypeEditor 类的派生来实现编辑器的调用,所以我们必须实现一个下拉式编辑器。代码如下:
      
None.gif using  System;
None.gif
using  System.ComponentModel;
None.gif
using  System.Drawing.Design;
None.gif
using  System.Windows.Forms.Design;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
namespace  CustomControlSample
ExpandedBlockStart.gif
{
InBlock.gif    
public class ScopeDropDownEditor : UITypeEditor
ExpandedSubBlockStart.gif    
{
InBlock.gif        
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
if (context != null && context.Instance != null)
ExpandedSubBlockStart.gif            
{
InBlock.gif                
return UITypeEditorEditStyle.DropDown;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return base.GetEditStyle(context);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
ExpandedSubBlockStart.gif        
{
InBlock.gif            IWindowsFormsEditorService editorService 
= null;
InBlock.gif
InBlock.gif            
if (context != null && context.Instance != null && provider != null)
ExpandedSubBlockStart.gif            
{
InBlock.gif                editorService 
= (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
InBlock.gif                
if (editorService != null)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    MyListControl control 
= (MyListControl)context.Instance;
InBlock.gif                    ScopeEditorControl editorControl 
= new ScopeEditorControl(control.Scope);
InBlock.gif                    editorService.DropDownControl(editorControl);
InBlock.gif                    value 
= editorControl.Scope;
InBlock.gif                    
return value;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return value;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

      看过上一篇文章的朋友应该对这段代码很熟悉,是的,这两个编辑器的代码只有几行不同之处,在GetEditStyle方法中,我们返回的是UITypeEditorEditStyle.DropDown,而不是UITypeEditorEditStyle.Modal,表明我们的编辑器是一个下拉式的编辑器。在EditValue中的不同之处是,我们使用DropDownControl方法来显示编辑器。编辑器制作完毕,我们把Scope以前的编辑器替换成下拉式编辑器,如下:
      

None.gif [Browsable( true )]
None.gif        [Editor(
typeof (ScopeDropDownEditor),  typeof (UITypeEditor))]
None.gif        
public  Scope Scope
ExpandedBlockStart.gif        
{
InBlock.gif            
get
ExpandedSubBlockStart.gif            
{
InBlock.gif                
return _scope;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gif            
{
InBlock.gif                _scope 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

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

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

   好了,属性的编辑到这里就讲完了。






本文转自纶巾客博客园博客,原文链接:http://www.cnblogs.com/guanjinke/archive/2006/12/19/597260.html,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
JSON 数据可视化 图形学
Graphix: 轻量级、可插拔、OOP 式图形编辑器开发引擎
A lightweight, pluggable, object-oriented programming (OOP) style graphic editor development engine / 一个轻量级、可插拔、OOP 式图形编辑器开发引擎
59 2
|
13天前
|
API 开发工具 C++
【专栏:工具与技巧篇】使用代码编辑器(VS Code/Sublime Text)提升开发效率
【4月更文挑战第30天】在VS Code与Sublime Text两大流行代码编辑器中,开发者可借助其高效特性提升编程效率。VS Code拥有丰富的插件生态、内置Git集成、强大的调试工具、智能提示和多文件导航功能。Sublime Text则以其轻量级、快速响应、多光标编辑及自定义构建系统见长。学习编辑器的键盘快捷键、自定义配置、使用内置终端以及键绑定和宏,将助开发者进一步提高开发效率。选择适合自己的编辑器并不断适应新技术是提升开发工作流的关键。
|
2月前
|
前端开发 Java Maven
属性编辑器未在PropertyEditorManager中注册?
属性编辑器未在PropertyEditorManager中注册?
13 0
|
10月前
|
前端开发 JavaScript API
wangEditor富文本编辑器的调用开发实录(v5版本、获取HTML内容、上传图片、隐藏上传视频)
wangEditor富文本编辑器的调用开发实录(v5版本、获取HTML内容、上传图片、隐藏上传视频)
513 0
|
5月前
|
存储 前端开发 JavaScript
前后端分离模式开发的BS电子病历编辑器源码(Java)
前后端分离模式开发的BS电子病历编辑器源码(Java)
54 0
|
6月前
|
前端开发 C# 开发工具
Unity快手上手【熟悉unity编辑器,C#脚本控制组件一些属性之类的】
Unity快手上手【熟悉unity编辑器,C#脚本控制组件一些属性之类的】
110 0
|
10月前
|
数据库
wangEditor富文本编辑器的调用开发实录2(V5版本自定义粘贴,去除复制word或网页html冗余样式代码的解决方案)
wangEditor富文本编辑器的调用开发实录2(V5版本自定义粘贴,去除复制word或网页html冗余样式代码的解决方案)
390 0
|
10月前
|
JSON 数据可视化 定位技术
GIS开发:Maputnik地图样式编辑器
GIS开发:Maputnik地图样式编辑器
390 0
|
11月前
|
XML 监控 数据库
电子病历系统的核心:电子病历编辑器控件
电子病历系统的核心:电子病历编辑器控件 在医疗业务中,医护人员需要书写电子病历,填写麻醉记录单、健康体检表、随访记录表等,出具影像报告、检查报告,以及上报不良事件等,书写形式多样,这就要求电子病历编辑器要具备强大的文本编辑能力。
|
12月前
|
开发工具 C++ Python