人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

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

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

更新時間:2022-07-28 文章作者:未知 信息來源:網絡 閱讀次數:

  在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屬性控件還有很多優點,本文只是對其做了簡單介紹,希望能給讀者啟發。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 大渡口区| 岳西县| 漠河县| 鄢陵县| 铜梁县| 仲巴县| 海原县| 保定市| 加查县| 勐海县| 二连浩特市| 梁山县| 含山县| 临猗县| 项城市| 南陵县| 简阳市| 泗水县| 金川县| 静海县| 安化县| 康保县| 桑植县| 黔江区| 武定县| 高台县| 白山市| 衡阳市| 都兰县| 五常市| 新沂市| 洛阳市| 个旧市| 中超| 五台县| 尼勒克县| 罗甸县| 万源市| 措勤县| 龙岩市| 额尔古纳市|