首页 > 开发 > 综合 > 正文

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

2024-07-21 02:29:33
字体:
来源:转载
供稿:网友
中国最大的web开发资源网站及技术社区,
  前一章,我们创建了最简单的组件,今天讲讲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,我会找出原因,并在下章补上,也希望知道的朋友能告之。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表