Windows 窗体编程 如何:实现类型转换器 请参见 语言筛选器: 全部 语言筛选器: 多个 语言筛选器: Visual Basic 语言筛选器: C# 语言筛选器: C++ 语言筛选器: J# 语言筛选器: JScript Visual Basic(声明) Visual Basic(用法) C# C++ J# JScript
类型转换器可用于在数据类型之间转换值,并通过提供文本到值的转换或待选值的下拉列表来帮助在设计时配置属性。如果配置正确,通过使用 InstanceDescriptor 和 System.Reflection 对象来给设计器序列化系统提供生成在运行时初始化属性的代码所需的信息,类型转换器可以生成属性配置代码。
值翻译的类型转换器 类型转换器可用于字符串到值的转换,或用于在设计时和运行时进行数据类型之间的双向翻译。在宿主(如窗体设计器中的属性浏览器)中,类型转换器允许以文本形式向用户表示属性值,并且可以将用户输入的文本转换为相应数据类型的值。
大多数本机数据类型(Int32、String、枚举类型和其他类型)具有默认的类型转换器,提供从字符串到值的转换并执行验证检查。默认的类型转换器位于 System.ComponentModel 命名空间中,名为 TypeConverterNameConverter。当默认功能无法满足需要时,可以扩展类型转换器;当定义的自定义类型没有关联的类型转换器时,可以实现自定义类型转换器。
注意 TypeConverterAttribute 属性通常应用于属性或数据成员,以将其与类型转换器关联。如果将 TypeConverterAttribute 应用于类型,则不必将其再次应用于该类型的属性或数据成员。
类型转换器的实现不依赖于任何用户界面功能。因此,可在 Windows 窗体和 Web 窗体中应用同一个类型转换器。
实现能够将字符串翻译成点的简单类型转换器 定义一个从 TypeConverter 派生的类。
重写 CanConvertFrom 方法,指定转换器可从中转换的类型。此方法是重载方法。
重写实现转换的 ConvertFrom 方法。此方法是重载方法。
重写 CanConvertTo 方法,指定转换器可转换为哪种类型。转换为字符串类型不需要重写此方法。此方法是重载方法。
重写实现转换的 ConvertTo 方法。此方法是重载方法。
重写执行验证的 IsValid 方法。此方法是重载方法。
下面的代码示例实现了一个类型转换器,该转换器可以将 String 类型转换为 Point 类型,将 Point 转换为 String。此示例中不重写 CanConvertTo 和 IsValid 方法。
Visual Basic 复制代码 Option Explicit Option Strict
Imports System Imports System.ComponentModel Imports System.Globalization Imports System.Drawing
Public Class PointConverter Inherits TypeConverter ' Overrides the CanConvertFrom method of TypeConverter. ' The ITypeDescriptorContext interface provides the context for the ' conversion. Typically, this interface is used at design time to ' provide information about the design-time container. Public Overrides Overloads Function CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type) As Boolean If sourceType Is GetType(String) Then Return True End If Return MyBase.CanConvertFrom(context, sourceType) End Function ' Overrides the ConvertFrom method of TypeConverter. Public Overrides Overloads Function ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value As Object) As Object If TypeOf value Is String Then Dim v As String() = CStr(value).Split(New Char() {","c}) Return New Point(Integer.Parse(v(0)), Integer.Parse(v(1))) End If Return MyBase.ConvertFrom(context, culture, value) End Function ' Overrides the ConvertTo method of TypeConverter. Public Overrides Overloads Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object If destinationType Is GetType(String) Then Return CType(value, Point).X & "," & CType(value, Point).Y End If Return MyBase.ConvertTo(context, culture, value, destinationType) End Function End Class C# 复制代码 using System; using System.ComponentModel; using System.Globalization; using System.Drawing;
public class PointConverter : TypeConverter { // Overrides the CanConvertFrom method of TypeConverter. // The ITypeDescriptorContext interface provides the context for the // conversion. Typically, this interface is used at design time to // provide information about the design-time container. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) { return true; } return base.CanConvertFrom(context, sourceType); } // Overrides the ConvertFrom method of TypeConverter. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string[] v = ((string)value).Split(new char[] {','}); return new Point(int.Parse(v[0]), int.Parse(v[1])); } return base.ConvertFrom(context, culture, value); } // Overrides the ConvertTo method of TypeConverter. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { return ((Point)value).X + "," + ((Point)value).Y; } return base.ConvertTo(context, culture, value, destinationType); } }
向“属性”窗口提供标准值列表的类型转换器 类型转换器可以为“属性”窗口控件中的类型提供一个值列表。如果类型转换器为类型提供了一组标准值,“属性”窗口控件中关联类型的属性的值输入字段就会显示一个向下键,单击该键可显示值的列表,以便设置属性的值。
当在设计时环境属性浏览器中选中了与该类型转换器关联的类型属性时,值输入字段将包含一个按钮,该按钮显示属性类型的标准值下拉列表,可从中选择标准值。
实现在属性浏览器中提供标准值下拉列表的简单类型转换器 定义一个从 TypeConverter 派生的类。
重写 GetStandardValuesSupported 方法并返回 true。
重写 GetStandardValues 方法并返回包含属性类型标准值的 StandardValuesCollection。属性的标准值必须与属性自身的类型一致。
重写 CanConvertFrom 方法并为类型字符串的 sourceType 参数值返回 true。
重写 ConvertFrom 方法并基于 value 参数返回相应的属性值。
将指示类型转换器类型的 TypeConverterAttribute 应用于要为其提供一组标准值的类型。
|