闪客动漫天地论坛

首页 » 设计乐园 » 彩信制作乐园 » 向“属性”窗口提供标准值列表的类型转换器
女女 - 2007-6-19 10:44:00
向“属性”窗口提供标准值列表的类型转换器
类型转换器可以为“属性”窗口控件中的类型提供一个值列表。如果类型转换器为类型提供了一组标准值,“属性”窗口控件中关联类型的属性的值输入字段就会显示一个向下键,单击该键可显示值的列表,以便设置属性的值。

当在设计时环境属性浏览器中选中了与该类型转换器关联的类型属性时,值输入字段将包含一个按钮,该按钮显示属性类型的标准值下拉列表,可从中选择标准值。

实现在属性浏览器中提供标准值下拉列表的简单类型转换器
定义一个从 TypeConverter 派生的类。

重写 GetStandardValuesSupported 方法并返回 true。

重写 GetStandardValues 方法并返回包含属性类型标准值的 StandardValuesCollection。属性的标准值必须与属性自身的类型一致。

重写 CanConvertFrom 方法并为类型字符串的 sourceType 参数值返回 true。

重写 ConvertFrom 方法并基于 value 参数返回相应的属性值。

将指示类型转换器类型的 TypeConverterAttribute 应用于要为其提供一组标准值的类型。

下面的示例演示了一个类型转换器,该转换器为其关联的类型属性的“属性”窗口控件提供了一组标准值。示例类型转换器支持与其关联的 integer 类型的属性。若要在 Visual Studio .NET 中使用该示例,请将该代码编译为类库,并向“工具箱”添加 IntStandardValuesControl 组件。在设计模式中将 IntStandardValuesControl 的实例添加到窗体,并在选中了控件的情况下在“属性”窗口中滚动到 TestInt 属性。选中属性的值输入字段,这时会出现一个向下键,单击该键可显示标准值的下拉列表。输入整数值将会把该值添加到标准值列表,并将属性设置为指定值。

C#  复制代码
using System;
using System.ComponentModel;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace StandardValuesTest

    public class StandardValuesIntConverter : System.ComponentModel.TypeConverter
    {
        private ArrayList values;
        public StandardValuesIntConverter()
        {
            // Initializes the standard values list with defaults.
            values = new ArrayList(new int[] { 1, 2, 3, 4, 5 });
        }

        // Indicates this converter provides a list of standard values.
        public override bool GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)
        {
            return true;
        }

        // Returns a StandardValuesCollection of standard value objects.
        public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(System.ComponentModel.ITypeDescriptorContext context)
        {       
            // Passes the local integer array.
            StandardValuesCollection svc =
                new StandardValuesCollection(values);     
            return svc;
        }

        // Returns true for a sourceType of string to indicate that
        // conversions from string to integer are supported. (The
        // GetStandardValues method requires a string to native type
        // conversion because the items in the drop-down list are
        // translated to string.)
        public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
        {
            if( sourceType == typeof(string) )
                return true;
            else
                return base.CanConvertFrom(context, sourceType);
        }

        // If the type of the value to convert is string, parses the string
        // and returns the integer to set the value of the property to.
        // This example first extends the integer array that supplies the
        // standard values collection if the user-entered value is not
        // already in the array.
        public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if( value.GetType() == typeof(string) )
            {
                // Parses the string to get the integer to set to the property.
                int newVal = int.Parse((string)value);
           
                // Tests whether new integer is already in the list.
                if( !values.Contains(newVal) )
                {
                    // If the integer is not in list, adds it in order.
                    values.Add(newVal);
                    values.Sort();
                }                               
                // Returns the integer value to assign to the property.
                return newVal;
            }
            else
                return base.ConvertFrom(context, culture, value);
        }
    }

    // Provides a test control with an integer property associated with
    // the StandardValuesIntConverter type converter.
    public class IntStandardValuesControl : System.Windows.Forms.UserControl
    {
        [TypeConverter(typeof(StandardValuesIntConverter))]
        public int TestInt
        {
            get
            {
                return this.integer_field;
            }
            set
            {
                if(value.GetType() == typeof(int))
                    this.integer_field = value;
            }
        }
        private int integer_field = 0;
     
        public IntStandardValuesControl()
        {
            this.BackColor = Color.White;
            this.Size = new Size(472, 80);
        }

        // OnPaint override displays instructions for the example.
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            if(this.DesignMode)
            {
                e.Graphics.DrawString("TypeConverter.GetStandardValues Example Control", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Blue), 5, 5);
                e.Graphics.DrawString("The type converter for the TestInt property of this", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Black), 5, 20);
                e.Graphics.DrawString("component provides a list of standard values to the", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Black), 5, 30);
                e.Graphics.DrawString("Properties window. Setting a value through a property", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Black), 5, 40);
                e.Graphics.DrawString("grid adds it to the list of standard values.", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Black), 5, 50);           
            }
            else
            {
                e.Graphics.DrawString("TypeConverter.GetStandardValues Example Control", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Blue), 5, 5);       
                e.Graphics.DrawString("This control was intended for use in design mode.", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Black), 5, 20);     
            }
        }
    }
}
 1 
查看完整版本: 向“属性”窗口提供标准值列表的类型转换器