Dot Net WinForm 控件开发 (三) 自定义类型的属性需要自定义类型转换器

      什么叫类型转换器?
.NET Framework 类库为常见数据类型(如整数、小数、布尔值和其他数据)提供了类型转换器。类型转换器的目的通常是用来提供字符串到数值的转换(从字符串数据转换为其他数据类型)。常见数据类型与默认类型转换器(将数值转换为字符串,并将字符串转换为相应数据类型)相关联。如果定义了自定义(即,非标准)数据类型的属性,则应用的属性必须将类型转换器指定为与该属性相关联。
      前篇文章中的为何没有用到, 其实它也用到了, 因为它用的是系统自带的类型int, 类型转换器已经由系统自动提供了.
     如果我们使用了自己定义的类型, 因为系统中没有相应的类型转换器, 这就需要我们写一个类型转换器.

     下面我们写一个稍稍复杂点的属性, 它是由简单的类型加简单的属性组合而成的,(没有晕吧),
也就是说我要自已定义一个类型, 而不用系统自带的类型(比如前篇文章中的int类型)

      下面就是拥有一个简单的复杂属性的简单控件, smile.gif
  1 None.gif using  System.ComponentModel;
  2 None.gif using  System.Windows.Forms;
  3 None.gif using  System.Drawing;
  4 None.gif
  5 None.gif namespace  CustomControlSample
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7InBlock.gif    public class SimpleComplexProperty : Control
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  9InBlock.gif        private SimpleCustomType complexField;
 10InBlock.gif
 11InBlock.gif        [Category("我是复杂的属性哦!")]
 12InBlock.gif        [Description("我是简单的复杂属性,因为我是由简单的类型和简单的方式定义的。\n定义我的类型很简单,只有两个属性(Min, Max);定义我的body也很简单,只是简单的get, set.")]
 13InBlock.gif        public SimpleCustomType ComplexProperty
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 15ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn complexField; }
 16ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ complexField = value; }
 17ExpandedSubBlockEnd.gif        }

 18InBlock.gif
 19InBlock.gif        protected override void OnPaint(PaintEventArgs e)
 20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 21InBlock.gif            base.OnPaint(e);
 22InBlock.gif            e.Graphics.DrawRectangle(Pens.Red, new Rectangle(Point.Empty, new Size(Width - 1, Height - 1)));
 23ExpandedSubBlockEnd.gif        }

 24InBlock.gif
 25ExpandedSubBlockEnd.gif    }

 26InBlock.gif
 27ContractedSubBlock.gifExpandedSubBlockStart.gif    简单的自定义类型#region 简单的自定义类型
 28ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 29InBlock.gif    /// 简单的自定义类型
 30ExpandedSubBlockEnd.gif    /// </summary>

 31InBlock.gif    public class SimpleCustomType
 32ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 33InBlock.gif        private int _min;
 34InBlock.gif        private int _max;
 35InBlock.gif
 36InBlock.gif        public int Min
 37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 38ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _min; }
 39ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _min = value; }
 40ExpandedSubBlockEnd.gif        }

 41InBlock.gif        public int Max
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 43ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _max; }
 44ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _max = value; }
 45ExpandedSubBlockEnd.gif        }

 46InBlock.gif
 47ExpandedSubBlockStart.gifContractedSubBlock.gif        public SimpleCustomType() dot.gif{ }
 48InBlock.gif
 49InBlock.gif        public SimpleCustomType(int min, int max)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 51InBlock.gif            _min = min;
 52InBlock.gif            _max = max;
 53ExpandedSubBlockEnd.gif        }

 54ExpandedSubBlockEnd.gif    }

 55ExpandedSubBlockEnd.gif    #endregion

 56InBlock.gif
 57ContractedSubBlock.gifExpandedSubBlockStart.gif    简单的自定义类型的类型转换器#region 简单的自定义类型的类型转换器
 58ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 59InBlock.gif    /// 简单的自定义类型的类型转换器
 60ExpandedSubBlockEnd.gif    /// </summary>

 61InBlock.gif    public class SimpleCustomTypeConverter : TypeConverter
 62ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 63InBlock.gif        public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
 64ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 65InBlock.gif            // 允许字符串类型转换为我们自定义的类型
 66InBlock.gif            if (sourceType == typeof(string))
 67InBlock.gif                return true;
 68InBlock.gif            return base.CanConvertFrom(context, sourceType);
 69ExpandedSubBlockEnd.gif        }

 70InBlock.gif
 71InBlock.gif        public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 73InBlock.gif            // 允许我们自定义的类型转换为字符串类型
 74InBlock.gif            if (destinationType == typeof(string))
 75InBlock.gif                return true;
 76InBlock.gif            return base.CanConvertTo(context, destinationType);
 77ExpandedSubBlockEnd.gif        }

 78InBlock.gif
 79InBlock.gif        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 81InBlock.gif            // 字符串类型的值如何转换成我们自定义的类型呢?看下面
 82InBlock.gif            if (value is string)
 83ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 84ExpandedSubBlockStart.gifContractedSubBlock.gif                string[] strs = ((string)value).Split(new char[] dot.gif',' });
 85InBlock.gif                SimpleCustomType sct = new SimpleCustomType();
 86InBlock.gif                sct.Min = int.Parse(strs[0]);
 87InBlock.gif                sct.Max = int.Parse(strs[1]);
 88InBlock.gif                return sct;
 89ExpandedSubBlockEnd.gif            }

 90InBlock.gif            return base.ConvertFrom(context, culture, value);
 91ExpandedSubBlockEnd.gif        }

 92InBlock.gif
 93InBlock.gif        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 95InBlock.gif            // 我们自定义类型的实例如何转换成字符串类型呢?看下面
 96InBlock.gif            if (destinationType == typeof(string))
 97ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 98InBlock.gif                if (value is SimpleCustomType)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
100InBlock.gif                    SimpleCustomType sct = (SimpleCustomType)value;
101InBlock.gif                    return sct.Min.ToString() + "" + sct.Max.ToString();
102ExpandedSubBlockEnd.gif                }

103ExpandedSubBlockEnd.gif            }

104InBlock.gif            return base.ConvertTo(context, culture, value, destinationType);
105ExpandedSubBlockEnd.gif        }

106ExpandedSubBlockEnd.gif    }

107ExpandedSubBlockEnd.gif    #endregion

108ExpandedBlockEnd.gif}

109 None.gif

编译, 拖到windows窗体上,点击查看属性浏览器,


wow, 是灰色的,不能使用. 为啥?
..... 那是因为属性浏览器不知道如何转换我的属性,
我们不是写了类型转换器了吗? 没有被使用, ...
又要用到Attribute了,这真是个好东西呀
在上面的代码中的属性ComplexProperty 用TypeConverter (TypeConverterAttribute的缩写)指定一下我们自定义的类型的类型转换器即可.
None.gif         [TypeConverter( typeof (SimpleCustomTypeConverter))]
None.gif        
public  SimpleCustomType ComplexProperty
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn complexField; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ complexField = value; }
ExpandedBlockEnd.gif        }

再编译......查看属性浏览器


OK了
The end.

转载于:https://www.cnblogs.com/luqingfei/archive/2007/03/14/674682.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值