盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

自定义组件之属性(Property)的性质(Attribute)介绍(4)

时间:2025/1/5作者:未知来源:盾怪网教程人气:

[摘要]二:UI属性编辑器(UITypeEditor)这里的属性编辑器的意思是能够实现上面提到的弹出对话框和下拉UI的形式。废话不说下面我们一一介绍。1、 弹出对话框的形式在本例中我使用了string类型...
二:UI属性编辑器(UITypeEditor)

这里的属性编辑器的意思是能够实现上面提到的弹出对话框和下拉UI的形式。废话不说下面我们一一介绍。

1、 弹出对话框的形式

在本例中我使用了string类型的属性来显示版本的信息,大家可以随便的写各类的属性,这里只需要指定改属性的编辑器就可以了。

首先我们要建立一个string类型的属性,代码如下:

private string _appVer="1.0";



[CategoryAttribute("自定义编辑器"),

DefaultValueAttribute("1.0"),

DescriptionAttribute("版本信息"),

ReadOnlyAttribute(true),

EditorAttribute(typeof(AppVerConverter),typeof(System.Drawing.Design.UITypeEditor))]

public string AppVer

{

get {return this._appVer;}

set {this._appVer=value;}

}

大家可能已经注意到了在这个属性之多出了一个性质EditorAttribute(typeof(AppVerConverter),typeof(System.Drawing.Design.UITypeEditor)),具体的意思大家可以参考MSDN我在这里就不用多说了,那么我们看看AppVerConverter这个类是怎么实现的就可以了。具体代码如下:

/// <summary>

/// 自定义UI的属性编辑器(弹出消息)

/// </summary>

public class AppVerConverter:System.Drawing.Design.UITypeEditor

{

/// <summary>

/// 覆盖此方法以返回编辑器的类型。

/// </summary>

public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)

{

return System.Drawing.Design.UITypeEditorEditStyle.Modal;

}



/// <summary>

/// 覆盖此方法以显示版本信息

/// </summary>

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

{

System.Windows.Forms.MessageBox.Show("版本:1.0\n作者:张翔","版本信息");

return value;

}

}

这里需要说明的是我们的属性编辑器必须从System.Drawing.Design.UITypeEditor继承,要不然就不能显示UI了。UITypeEditorEditStyle方法的返回值决定了改属性编辑器的类型大家可以参考msdn我在这里就不多说了。编译之后就可以看到如下的画面了:




2、 下拉UI的类型

下拉UI类型主要是提供给用户一个简单的界面来选择所要确定的属性,这种方式提供给用户非常友好的界面。下面的例子我们首先定义里一个Point类型的属性,在默认的情况下这种类型的属性是会以展开的形式来让用户编辑的。在这里我们扩展了他的功能,不仅仅能通过直接输入的方式来改变值,而且还可以下拉出来一个控件,用户可以在这个控件上根据鼠标的位置来确定具体的值。下面具体的代码:

private System.Drawing.Point _dropUI;



[CategoryAttribute("自定义编辑器"),

DefaultValueAttribute("1"),

DescriptionAttribute("下拉可视控件"),

ReadOnlyAttribute(false),

EditorAttribute(typeof(DropEditor),typeof(System.Drawing.Design.UITypeEditor))]

public System.Drawing.Point DropUI

{

get { return this._dropUI;}

set { this._dropUI=value; }

}







public class DropEditor:System.Drawing.Design.UITypeEditor

{



public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)

{

return System.Drawing.Design.UITypeEditorEditStyle.DropDown;

}



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

{

System.Windows.Forms.Design.IWindowsFormsEditorService iws=(System.Windows.Forms.Design.IWindowsFormsEditorService)provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService));

if (iws!=null)

{

PropertyGridApp.DropUIControl UIControl=new PropertyGridApp.DropUIControl((System.Drawing.Point)value,iws);

iws.DropDownControl(UIControl);

return UIControl.Value;

}

return value;

}

}



internal class DropUIControl:System.Windows.Forms.UserControl

{

public DropUIControl(System.Drawing.Point avalue,System.Windows.Forms.Design.IWindowsFormsEditorService iws)

{

this.Value=avalue;

this._tmpvalue=avalue;

this._iws=iws;

this.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer

关键词:自定义组件之属性(Property)的性质(Attribute)介绍(4)




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版