文章分类 | 软件分类 | 最新软件 | 杀毒软件 | 实用软件  | MTV下载  | 设为首页 |
  | 下载分类 | 最近更新
您的位置: 首页 >> 文章首页 >> 技术开发 >> .Net 专栏 >> C#语言 >>  
C#语言点击TOP10
·使用Web服务将C#代码转换为VB.NET代码2006-2-9 18:10:39
·C#编写的windows计算器-源代码2006-2-10 16:08:20
·用Visual C#中实现DB2数据库编程2006-2-9 16:41:48
·C#中的非安全编程2006-2-9 17:32:38
·编程特例篇——LOGO语言2006-2-5 23:53:56
·Visual FoxPro 9中新的数据处理方式2006-2-6 7:50:17
·小议Windows CE 的下浏览器配置2006-2-6 7:50:09
·如何更好更快的debug2006-2-5 23:53:54
·利用c#制作简单的留言板22006-2-9 16:41:33
·C++中通过溢出覆盖虚函数指针列表执行代码2006-2-9 12:28:20
.Net 专栏点击TOP10
·ADO.net中数据库连接方式2006-2-9 17:35:03
·ASP.NET里的事务处理2006-2-5 12:41:30
·使用Web服务将C#代码转换为VB.NET代码2006-2-9 18:10:39
·十天学会ASP.net之第九天2006-2-6 10:10:06
·如何给DataGrid添加双题头分类显示2006-2-5 18:46:43
·使用嵌套的Repeater控件2006-2-5 12:41:52
·Net 下安装、调试的常见问题与错误2006-2-9 9:04:53
·在ASP.NET中值得注意的两个地方2006-2-9 16:40:07
·C#中的非安全编程2006-2-9 17:32:38
·ASP.NET中利用cookies保持客户端信息2006-2-9 17:33:25

 

在C#中使用属性控件添加属性窗口
作者:我去下载           时间:2006-2-9 17:42:08


  在VS.NET 中,我们可以很方便地使用属性窗口来对某个控件的属性进行设置,那么,我们有没有想过,如果在应用程序中,在对程序中的自定义的属性进行设置时,显示一个象属性窗口一样的窗体,能对其中的属性方便的设置呢?就象下图所示的一样。



  答案是完全可以的。我们可以使用微软提供的property属性控件来实现该功能。首先,我们新建一个c#的windows应用程序,之后在工具箱中,鼠标右键点选工具箱(TOOLBOX),在弹出的菜单中选择“添加/移除项”,如下图所示:


  在弹出的窗口中,选择.NET Freamwork components窗口,再选择其中的property grid控件,点击选择就完成了对控件的加入,如下图所示:

  现在,我们可以开始使用该控件了。第一步,创建在应用程序中将要展现的字段属性为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属性控件还有很多优点,本文只是对其做了简单介绍,希望能给读者启发。

分页:
相关文章:
Copyright© 2005-2006 wqxz.com, All Rights Reserved. 购买虚拟主机请与本站联系