艾伟:WinForm控件开发总结(五)-----为控件的复杂属性提供类型转换器

简介: 上一篇文章我已经介绍了TypeConverterAttribute元数据的作用,本文将通过代码向你展示具体的实现。在这个例子中,我要给控件添加一个复杂的属性,这个属性对这个控件没有什么功用,纯粹是为了演示,有些牵强附会了。

      上一篇文章我已经介绍了TypeConverterAttribute元数据的作用,本文将通过代码向你展示具体的实现。在这个例子中,我要给控件添加一个复杂的属性,这个属性对这个控件没有什么功用,纯粹是为了演示,有些牵强附会了。
       现在在前一篇文章中的创建的控件代码中添加一个Scope属性:
      

        [Browsable(true)]
        public Scope Scope
         {
            get
             {
                return _scope;
            }
            set
             {
                _scope = value;
            }
        }
      这个属性的类型是Scope类,代码如下:
public class Scope
     {
        private Int32 _min;
        private Int32 _max;

        public Scope()
         {
        }

public Scope(Int32 min, Int32 max)
         {
            _min = min;
            _max = max;
        }

        [Browsable(true)]
        public Int32 Min
         {
            get
             {
                return _min;
            }
            set
             {
                _min = value;
            }
        }

        [Browsable(true)]
        public Int32 Max
         {
            get
             {
                return _max;
            }
            set
             {
                _max = value;
            }
           
        }
}

       添加完属性后,build控件工程,然后在测试的工程里选中添加的控件,然后在属性浏览器里观察它的属性,发现Scope属性是灰的,不能编辑。前一篇文章提到了,在属性浏览器里可以编辑的属性都是有类型转换器的,而.NET框架为基本的类型和常用的类型都提供了默认的类型转换器。接下来我们为Scope类添加一个类型转换器,以便这个属性能够被编辑,而且也可以在源代码文件里自动生成相应的代码。下面是类型转换器的代码:

public class ScopeConverter : TypeConverter
     {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
         {
            if (sourceType == typeof(String)) return true;

            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
         {
            if (destinationType == typeof(String)) return true;

            if (destinationType == typeof(InstanceDescriptor)) return true;

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
         {
            String result = "";
            if (destinationType == typeof(String))
             {
                Scope scope = (Scope)value;
                result = scope.Min.ToString()+"," + scope.Max.ToString();
                return result;

            }

            if (destinationType == typeof(InstanceDescriptor))
             {
                ConstructorInfo ci = typeof(Scope).GetConstructor(new Type[]  {typeof(Int32),typeof(Int32) });
                Scope scope = (Scope)value;
                return new InstanceDescriptor(ci, new object[]  { scope.Min,scope.Max });
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
         {
            if (value is string)
             {
                String[] v = ((String)value).Split(',');
                if (v.GetLength(0) != 2)
                 {
                    throw new ArgumentException("Invalid parameter format");
                }

                Scope csf = new Scope();
                csf.Min = Convert.ToInt32(v[0]);
                csf.Max = Convert.ToInt32(v[1]);
                return csf;
            }
            return base.ConvertFrom(context, culture, value);
        }
    }

      现在我们为类型提供类型转换器,我们在类型前面添加一个TypeConverterAttribute,如下:
           
    [TypeConverter(typeof(ScopeConverter))]
    public class Scope
        添加完以后build工程,然后切换到测试工程,选中控件,在属性浏览器里查看属性,现在的Scope属性可以编辑了,如下图所示: 
         
         我们修改默认的值,然后看看Form设计器为我们生成了什么代码:
      
this.myListControl1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.myListControl1.Item.Add(1);
            this.myListControl1.Item.Add(2);
            this.myListControl1.Item.Add(3);
            this.myListControl1.Item.Add(6);
            this.myListControl1.Item.Add(8);
            this.myListControl1.Item.Add(9);
            this.myListControl1.Location = new System.Drawing.Point(12, 34);
            this.myListControl1.Name = "myListControl1";
            this.myListControl1.Scope = new CustomControlSample.Scope(10, 200);
            this.myListControl1.Size = new System.Drawing.Size(220, 180);
            this.myListControl1.TabIndex = 1;
        this.myListControl1.Text = "myListControl1";
        关键是这一行this.myListControl1.Scope = new CustomControlSample.Scope(10, 200),Scope类的类型转换器为属性提供了实例化的代码。      
目录
相关文章
|
算法 前端开发
1873. 计算特殊奖金
1873. 计算特殊奖金
166 0
|
机器学习/深度学习 人工智能 自然语言处理
【自然语言处理】python之人工智能应用篇——文本生成技术
文本生成是指使用自然语言处理技术,基于给定的上下文或主题自动生成人类可读的文本。这种技术可以应用于各种领域,如自动写作、聊天机器人、新闻生成、广告文案创作等。
1298 8
|
机器学习/深度学习 数据可视化
R语言逻辑回归分析连续变量和分类变量之间的“相关性“
R语言逻辑回归分析连续变量和分类变量之间的“相关性“
|
人工智能 运维 Cloud Native
云计算招聘岗位全方位需求解析+学习路径指南
在2020阿里云峰会上,阿里云智能总裁张建锋表示,阿里云今年再招5000人,大规模引进顶尖科技人才。7月9日 19:00-21:30 阿里云开发者社区首场“Offer 5000”直播开启!15位团队技术大牛在线招人,《阿里云技术面试红宝书》助你拿下Offer!马上投递简历: https://developer.aliyun.com/special/offerday01,在此,小编奉上云计算招聘岗位全方位需求解析+学习路径指南,帮你拿下好工作!
云计算招聘岗位全方位需求解析+学习路径指南
|
C语言 Windows
Windows上C语言实现设置控制台的颜色
这里设置只要调用system();这个函数就行了,参数是字符串,想要设置终端的颜色,按照以下格式可以设置: //数字表示前景色,字母表示背景色 //color 0=黑色 1蓝色 2 绿色 3湖蓝色 4红色 5紫色 6黄色 7白色 8灰色 9淡蓝色 // A淡绿色 B淡浅绿色 C淡红色 D淡紫色 E淡黄色 F亮白色 所以,在程序中,只要写system("color 2D");就代表设置前景色为绿色,背景色为淡紫色。
1467 0
|
1天前
|
云安全 安全 网络安全
|
1天前
|
JavaScript 安全 前端开发
Nodejs Brotli decoder 代码执行漏洞 (CVE-2026-111497)10629
Node.js发布安全更新修复Brotli解码器高危RCE漏洞(CVE-2026-111497),建议受影响版本用户立即升级。
1644 0
|
4天前
|
存储 人工智能 自然语言处理
阿里云千问办公官网登录入口:qwenwork.cn网页版免下载,注册送2000积分、登录每日再送100积分
千问办公(QwenWork)是阿里巴巴推出的一站式AI办公平台,定位"不止于对话,更注重交付"。它基于千问大模型,具备任务拆解与多步骤自动执行能力,用户一句话即可完成PPT生成、数据分析、网页搭建、视频剪辑、信息调研等复杂任务,覆盖网页端、桌面端及钉钉生态。网页版免下载,访问qwenwork.cn注册即得2000积分,每日登录再送100积分。个人版免费起用,另有付费版与企业版可选。
|
8天前
|
人工智能 API 开发工具
DeepSeek Harness 神插件:白嫖 13 家大厂模型
`dsh-our-free-model` 是 DeepSeek Harness 神级插件:零配置、零API Key、零账号,聚合13家大厂免费模型(含DeepSeek V4.1 Flash、Kimi K3等),支持本地网关、用量看板与自动降级,真正实现“白嫖即用”。
1335 29

热门文章

最新文章