转自原文 在C#中使用属性控件添加属性窗口

第一步,创建在应用程序中将要展现的字段属性为public公有属性。其中,所有的属性必须有get和set的方法(如果不设置get方法,则要显示的属性不会显示在属性控件中)。为了设置相关的属性,必须设置下面的一些关于属性控件的属性值,如下表所示:

属性值 含义
CategoryAttribute 该属性对在Property控件中的属性按字母顺序进行归类
DescriptionAttribute 其值为对每个属性的具体文字描述,将会显示在property控件的底部
BrowsableAttribute 该值为是否在property控件中显示或者隐藏某个属性
ReadOnlyAttribute 该值为某个属性值是否在property控件中只读
DefaultValueAttribute 每个属性的默认值

  接下来,我们创建一个用户类,并且使用属性控件,使得可以在属性控件框中改变其值。我们先引入相关的命名空间:

using System.ComponentModel;

  之后,创建相关的类,设置有关的属性,代码如下:

/// Customer class to be displayed in the property grid

/// </summary>

/// [DefaultPropertyAttribute("Name")]

public class Customer
{
 private string _name;
 private int _age;
 private DateTime _dateOfBirth;
 private string _SSN;
 private string _address;
 private string _email;
 private bool _frequentBuyer;
 [CategoryAttribute("ID Settings"), DescriptionAttribute("Name of the customer")]   

public string Name
 {
  get
  {
   return _name;
  }
  set
  {
   _name = value;
  }
 }
 [CategoryAttribute("ID Settings"), DescriptionAttribute("Social Security Number of the customer")]

 public string SSN
 {
  get
  {
   return _SSN;
  }
  set
  {
   _SSN = value;
  }
 }
 [CategoryAttribute("ID Settings"), DescriptionAttribute("Address of the customer")]
 public string Address
 {
  get
  {
   return _address;
  }
  set
  {
   _address = value;
  }
 }
 [CategoryAttribute("ID Settings"), DescriptionAttribute("Date of Birth of the Customer (optional)")]
 public DateTime DateOfBirth
 {
  get { return _dateOfBirth; }
  set { _dateOfBirth = value; }
 }
 [CategoryAttribute("ID Settings"), DescriptionAttribute("Age of the customer")]
 public int Age
 {
  get { return _age; }
  set { _age = value; }
 }
 [CategoryAttribute("Marketting
Settings"), DescriptionAttribute("If the customer has bought more than
10 times, this is set to true")]
 public bool FrequentBuyer
 {
  get { return _frequentBuyer; }
  set { _frequentBuyer = value; }
 }
 [CategoryAttribute("Marketting Settings"), DescriptionAttribute("Most current e-mail of the customer")]
 public string Email
 {
  get { return _email; }
  set { _email = value; }
 }
 public Customer() { }
}

  可以看到,在上面的代码中,我们对customer类中的属性进行了设置,如姓名,出生日期,地址等。
接着,我们要为创建的customer类创建一个实例,并且将其与属性控件绑定。属性控件会自动根据类中对属性的相关设置,从而在界面中显示有关的属性,并且还可以进行编辑,比如,可以对生日属性进行修改,修改时会弹出日历控件框,十分方便。代码如下:

private void Form1_Load(object sender, System.EventArgs e)
{
//创建bill对象,实例化CUSTOMER类
Customer bill = new Customer();
//赋值给属性
bill.Age = 50;
bill.Address = " 114 Maple Drive ";
bill.DateOfBirth = Convert.ToDateTime(" 9/14/78");
bill.SSN = "123-345-3566";
bill.Email = “bill@aol.com”
bill.Name = "Bill Smith";
//将对象绑定到property控件中
propertyGrid1.SelectedObject = bill;
}

  最后,运行程序,我们就得到了本文一开始图示的结果了。再来回顾下该程序,其中我们使用了CatrgoryAttribute属性,定义了id

settings和MarketSettings,它们在属性控件中以分类的形式出现(注意它们前有个“+”号,点击可以展开看到其子属性)。同时,我们每当选择一个属性时,在属性控件框的下部,会同时显示该属性的相关描述。

  Property属性控件还有很多优点,本文只是对其做了简单介绍,希望能给读者启发。

在C#中使用属性控件添加属性窗口的更多相关文章

  1. m_Orchestrate learning system---二十六、动态给封装好的控件添加属性

    m_Orchestrate learning system---二十六.动态给封装好的控件添加属性 一.总结 一句话总结:比如我现在封装好了ueditor控件,我外部调用这个控件,因为要写数据到数据库 ...

  2. asp.net中的ListBox控件添加双击事件

    问题:在Aspx页里的ListBox A中添加双击事件,将选中项添加到另一个ListBox B中,双击ListBox B中的选中项,删除当前选中项 页面: <asp:ListBox ID=&qu ...

  3. repeater中后台动态为控件添加属性

    在此贴出repeater中的ItemDataBound事件中的代码: private void ItemDataBound(object sender, RepeaterItemEventArgs e ...

  4. MFC中关于CListBox控件添加水平滚动条

    首先是设置listbox控件的属性  Horizontal Scroll设为TRUE: 然后添加函数到CUighurRecognitionDlg.cpp(在CUighurRecognitionDlg. ...

  5. WPF中禁止WebBrowser控件打开新窗口

    一.针对纯WPF的WebBrowser控件: <summary> Suppress Script Errors In WPF WebBrowser </summary> pub ...

  6. Xcode UIView 中的Button 控件的属性和基本用法

      //第一种创建UIButton的方法 //initWhitFrame: UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(1 ...

  7. 009. C#中的WebBrowser控件的属性、方法及操作演示代码(转)

    本文转自 http://www.open-open.com/code/view/1430559996802 0.常用方法 Navigate(string urlString):浏览urlString表 ...

  8. 【深入篇】Andorid中常用的控件及属性

    TextView  android:autoLink 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/phone/map/al ...

  9. VS2008中为控件添加属性(比如前景色,背景色)

    VS2008中没有classwizard,但不要伤心,到了VS2010,classwizard又回来了. 可以参照这篇博客:http://blog.csdn.net/candyliuxj/articl ...

随机推荐

  1. cannot be deleted directly via the port API: has device owner network:floatingip

  2. Redis 实现用户积分排行榜

    排行榜功能是一个很普遍的需求.使用 Redis 中有序集合的特性来实现排行榜是又好又快的选择. 一般排行榜都是有实效性的,比如“用户积分榜”.如果没有实效性一直按照总榜来排,可能榜首总是几个老用户,对 ...

  3. git 绑定github

    1.创建一个名为git文件夹 2.git init 3.ssh-keygen -t rsa -C "邮箱地址" 4.根据上一步当中默认的文件夹找到id_rsa.pub 复制其中的内 ...

  4. 常用的Linux操作一

    Linux 常用的操作必须明白. 1.ls  和ll 列出文件的目录. 2.tail -f XXX  查看文件. 3.chmod -R 777 XXX.jar 赋予权限 4.cat 查看文件 -n 对 ...

  5. 第七讲:HTML5中的canvas两个小球全然弹性碰撞

    <html> <head> <title>小球之间的碰撞(全然弹性碰撞)</title> <script src="../js/jsce ...

  6. java 获取黑屏信息保存在list中,截取字符执行

    ArrayList<String> list1 = new ArrayList<String>(); Process p = Runtime.getRuntime().exec ...

  7. [转] 多线程下变量-gcc原子操作 __sync_fetch_and_add等

    http://blog.sina.com.cn/s/blog_6f5b220601013zw3.html 非常好的原子操作,不用加锁:__sync_fetch_and_add GCC 提供的原子操作 ...

  8. 实例化讲解 RunLoop

    实例化讲解RunLoop 之前看过很多有关RunLoop的文章,其中要么是主要介绍RunLoop的基本概念,要么是主要讲解RunLoop的底层原理,很少用真正的实例来讲解RunLoop的,这其中有大部 ...

  9. 给iOS开发者的GCD用户手册

    Grand Central Dispatch,或者GCD,是一个极其强大的工具.它给你一些底层的组件,像队列和信号量,让你可以通过一些有趣的方式来获得有用的多线程效果.可惜的是,这个基于C的API是一 ...

  10. 使用jQuery Mobile和Phone Gap开发Android应用程序(转)

    经过了一段时间的学习,初步了解了该如何使用jQuery Mobile和 Phone Gap来开发一个Android应用程序,也想把这些东西介绍给大家. 1. 软件准备 要进行android app的开 ...