.NET组件编程(2) PropertyAttribute和EventAttribute

  昨天晚上写了基础篇,有朋友说写的太简单,我想在这里申明下:因为我要写组件编程的完整系列,所以从最简单的开始写起,而且园子里有很多的朋友可能从来都没有写组件的经历,在这里希望有组件开发经验的朋友能多多包涵。
        前一章,我们创建了最简单的组件,今天讲讲Component的PropertyAttribute和EventAttribute。
        EventAttribute有:
                BrowsableAttribute 、CategoryAttribute、DescriptionAttribute、DefaultEventAttribute
        PropertyAttribute有:
                BrowsableAttribute 、CategoryAttribute、DescriptionAttribute、DefaultPropertyAttribute、DefaultValueAttribute、EditorAttribute 
、DesignerSerializationVisibilityAttribute、TypeConverterAttribute、BindableAttribute、LocalizableAttribute       
        在本章教程中我们主要讲以上红色的Attribute,再下章的Designer UI会讲蓝色的Attribute,紫色的Attribute不作讲解。
     上述的Attribute简明阐述如下:
             BrowsableAttribute:在Property窗口中是否可见。
             CategoryAttribute:Property或者Event所属的哪个组。
             DescriptionAttribute:Property或者Event的简单描述。
             DefaultEventAttribute:默认Event、。
             DefaultPropertyAttribute:默认Property,选中组件,其Property窗口中默认选中在这个Property上。
             DefaultValueAttribute:Property的默认值,选中组件,其Event窗口中默认选中在这个Event上。

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace Components
{
    // PropertyAttribute、EventAttribute分别放在Property、Event上,并[]括起来。
    // DefaultPropertyAttribute、DefaultEventAttribute必须放在类头上。
    [DefaultEvent("CustomerLogout")]  
    public class Customer : Component
    {
        private string _id;
        private string _sex;
        private int _age;
        private string _address;
        private DateTime _createTime;

        // 没有CategoryAttribute、DescriptionAttribute。
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        // 此属性在Customer's Details分组中,CategoryAttribute、DescriptionAttribute也适用于Event。
        [Category("Customer's Details"), Description("Customer's Sex")]  // 可以在一个[]里写两个Attribute。
        public string Sex
        {
            get { return _sex; }
            set { _sex = value; }
        }

        [Category("Customer's Details")]
        [Description("Customer's Age"), DefaultValue(20)]
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }

        [DefaultValue("shanghai"),Category("Customer's Details")]
        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }

        [Browsable(false)] // 此Property在Property窗口中不可见,BrowsableAttribute也适用于Event。
        public DateTime CreateTime
        {
            get { return _createTime; }
            set { _createTime = value; }
        }



        public sealed class CustomerLoginEventArgs : EventArgs
        { }
        public sealed class CustomerLogoutEventArgs : EventArgs
        { }

        public delegate void CustomerLoginEventHandler(object sender, CustomerLoginEventArgs e);
        public delegate void CustomerLogoutEventHandler(object sender, CustomerLogoutEventArgs e);

        public event CustomerLoginEventHandler CustomerLogin
        {
            add { }
            remove { }
        }

        public event CustomerLogoutEventHandler CustomerLogout
        {
            add { }
            remove { }
        }
    }
}

其Property、Event窗口如下:



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值