六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

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

[摘要]在VS.NET 中,我們可以很方便地使用屬性窗口來對某個控件的屬性進行設置,那么,我們有沒有想過,如果在應用程序中,在對程序中的自定義的屬性進行設置時,顯示一個象屬性窗口一樣的窗體,能對其中的屬性方便的設置呢?就象下圖所示的一樣。  答案是完全可以的。我們可以使用微軟提供的property屬性控件...
  在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 = “[email protected]
bill.Name = "Bill Smith";
//將對象綁定到property控件中
propertyGrid1.SelectedObject = bill;
}

  最后,運行程序,我們就得到了本文一開始圖示的結果了。再來回顧下該程序,其中我們使用了CatrgoryAttribute屬性,定義了id settings和MarketSettings,它們在屬性控件中以分類的形式出現(注意它們前有個“+”號,點擊可以展開看到其子屬性)。同時,我們每當選擇一個屬性時,在屬性控件框的下部,會同時顯示該屬性的相關描述。

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




主站蜘蛛池模板: 天天操夜夜草 | 天美视频在线观看 | 污视频在线看 | 手机福利视频一区二区 | 色哟哟色院91精品网站 | 日韩爱爱网 | 四虎a456tncom| 色一情一乱一乱91av | 欧美一级免费看 | 欧美一级黄色片子 | 天天看片天天操 | 香港三级理论在线播放1 | 欧美亚洲日本 | 日韩黄色在线视频 | 中文字幕亚洲色图 | 欧美又粗又大又硬又长又爽视频 | 色人久久| 日韩中文在线观看 | 最近手机中文字幕大全首页 | 日日摸夜夜添夜夜添特色大片 | 日韩黄色免费观看 | 亚洲调教 | 一级免费a | 青青草免费在线观看视频 | 欧美线人一区二区三区 | 日本激情在线观看 | 香蕉成人福利片视频在线下载 | 欧美在线综合视频 | 日韩伦理在线视频 | 青青草手机视频在线观看 | 色网站免费| 色噜噜狠狠狠色综合久 | 日本人欧美xx | 亚洲精品老司机 | 深夜福利视频导航 | 日韩欧美亚洲国产精品字幕久久久 | 伊人网免费视频 | 亚洲欧美精选 | 色综合综合在线 | 日日夜夜爱| 三级国产4国语三级在线 |