![]() |
答案是完万能够的。我们能够使用微软提供的property属性控件来实现该功能。首先,我们新建一个c#的windows应用程式,之后在工具箱中,鼠标右键点选工具箱(TOOLBOX),在弹出的菜单中选择“添加/移除项”,如下图所示:
![]() |
在弹出的窗口中,选择.NET Freamwork components窗口,再选择其中的property grid控件,点击选择就完成了对控件的加入,如下图所示:
![]() |
| 属性值 | 含义 |
| 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类创建一个实例,并且将其和属性控件绑定。属性控件会自动根据类中对属性的相关配置,从而在界面中显示有关的属性,并且还能够进行编辑,比如,能够对生日属性进行修改,修改时会弹出日历控件框,十分方便。代码如下:
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!







