首页产品库评测行情新闻|手机数码笔记本台式机DIY硬件数字家庭数码相机办公外设|软件下载游戏开发|社区

更多

数码相机
MP4
LCD
机箱
音箱

软件资讯设计 工具 系统 开发 安全 办公 陶吧 IT教育 Vista频道 | 下载中心酷我音乐盒 腾讯QQ
天极网 > 开发频道>深入浅出组件编程之固有属性和事件属性

深入浅出组件编程之固有属性和事件属性

2006-03-10 08:52作者:mapserver出处:BLOG责任编辑:方舟

  前一章,我们创建了最简单的组件,今天讲讲Component的PropertyAttribute和EventAttribute。

  EventAttribute有:

  BrowsableAttribute 、CategoryAttribute、DescriptionAttribute、DefaultEventAttribute

  PropertyAttribute有:

  BrowsableAttribute 、CategoryAttribute、DescriptionAttribute、DefaultPropertyAttribute、DefaultValueAttributeEditorAttribute、DesignerSerializationVisibilityAttribute、TypeConverterAttributeBindableAttribute、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窗口如下:




  我原来没有用过DefaultValueAttribute,上面代码中的Address、Age在Customer1创建时没有得到DefaultValue,我会找出原因,并在下章补上,也希望知道的朋友能告之。

关注此文的读者还看过:

返回开发频道首页

共1页。 1
  • 1.深入浅出组件编程之固有属性和事件属性

软件频道最新更新

热点推荐

IT嘉年华

编辑推荐

软件下载

热门
推荐

网友关注

软件
资料
游戏

装机推荐

文章排行

本周
本月
最新更新
天极服务|关于我们|About us|网站律师|RSS订阅|友情合作|加入我们|天极动态|网站地图|意见反馈|MSN/QQ上看天极
Copyright (C) 1999-2012 Yesky.com, All Rights Reserved 版权所有 天极网络