午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

在C#中使用屬性控件添加屬性窗口

 amine 2010-05-10
2007-01-02      來源:天極軟件頻道         作者:廖煜嶸編譯
關鍵詞:  控件     c#     使用     窗口     屬性  
在VS.NET 中,我們可以很方便地使用屬性窗口來對某個控件的屬性進行設置,那么,我們有沒有想過,如果在應用程序中,在對程序中的自定義的屬性進行設置時,顯示一個象屬性窗口一樣的窗體,能對其中的屬性方便的設置呢?就象下圖所示的一樣。



答案是完全可以的。我們可以使用微軟提供的property屬性控件來實現(xiàn)該功能。首先,我們新建一個c#的windows應用程序,之后在工具箱中,鼠標右鍵點選工具箱(TOOLBOX),在彈出的菜單中選擇“添加/移除項”,如下圖所示:


在彈出的窗口中,選擇.NET Freamwork components窗口,再選擇其中的property grid控件,點擊選擇就完成了對控件的加入,如下圖所示:


現(xiàn)在,我們可以開始使用該控件了。第一步,創(chuàng)建在應用程序中將要展現(xiàn)的字段屬性為public公有屬性。其中,所有的屬性必須有get和set的方法(如果不設置get方法,則要顯示的屬性不會顯示在屬性控件中)。為了設置相關的屬性,必須設置下面的一些關于屬性控件的屬性值,如下表所示:



屬性值含義
CategoryAttribute該屬性對在Property控件中的屬性按字母順序進行歸類
DescriptionAttribute其值為對每個屬性的具體文字描述,將會顯示在property控件的底部
BrowsableAttribute該值為是否在property控件中顯示或者隱藏某個屬性
ReadOnlyAttribute該值為某個屬性值是否在property控件中只讀
DefaultValueAttribute每個屬性的默認值

接下來,我們創(chuàng)建一個用戶類,并且使用屬性控件,使得可以在屬性控件框中改變其值。我們先引入相關的命名空間:

using System.ComponentModel;

之后,創(chuàng)建相關的類,設置有關的屬性,代碼如下:

/// 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類中的屬性進行了設置,如姓名,出生日期,地址等。
接著,我們要為創(chuàng)建的customer類創(chuàng)建一個實例,并且將其與屬性控件綁定。屬性控件會自動根據類中對屬性的相關設置,從而在界面中顯示有關的屬性,并且還可以進行編輯,比如,可以對生日屬性進行修改,修改時會彈出日歷控件框,十分方便。代碼如下:

private void Form1_Load(object sender, System.EventArgs e)
{
//創(chuàng)建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,它們在屬性控件中以分類的形式出現(xiàn)(注意它們前有個“+”號,點擊可以展開看到其子屬性)。同時,我們每當選擇一個屬性時,在屬性控件框的下部,會同時顯示該屬性的相關描述。

Property屬性控件還有很多優(yōu)點,本文只是對其做了簡單介紹,希望能給讀者啟發(fā)。

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多