日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解C#WinForm控件開發(fā)如何設置默認值

C# WinForm控件開發(fā)設置默認值是非常有必要的,實現(xiàn)起來也很容易,本文筆者為你介紹設置默認值的方法,希望能給你帶來幫助。

長沙縣網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、成都響應式網(wǎng)站建設公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選成都創(chuàng)新互聯(lián)

如果你為屬性設定了默認值,那么當開發(fā)者修改了屬性的值,這個值在Property Explorer中將會以粗體顯示。VS為屬性提供一個上下文菜單,允許程序員使用C# WinForm控件開發(fā)把值重置為默認值。

當Visual Studio進行控件的串行化時,他會判斷那些值不是默認值,只有不是設置默認值的屬性才會被串行化,所以為屬性提供設置默認值時可以大大減少串行化的屬性數(shù)目,提高效率。

那么Visual Studio進怎么知道我們的屬性值不是默認值了呢?我們需要一種機制來通知Visual Studio進默認值。實現(xiàn)這種機制有兩種方法:

對于簡單類型的屬性,比如Int32,Boolean等等這些Primitive類型,你可以在屬性的聲明前設置一個DefaultValueAttribute,在Attribute的構造函數(shù)里傳入設置默認值。

對于復雜的類型,比如Font,Color,你不能夠直接將這些類型的值傳遞給Attibute的構造函數(shù)。相反你應該提供Reset 和ShouldSerialize 方法,比如ResetBackgroundColor(),ShouldSerializeBackgroundColor()。

VS能夠根據(jù)方法的名稱來識別這種方法,比如Reset 方法把重置為設置默認值,ShouldSerialize 方法檢查屬性是否是設置默認值。過去我們把它稱之為魔術命名法,應該說是一種不好的編程習慣,可是現(xiàn)在微軟依然使用這種機制。我還是以前面幾篇文章使用的例子代碼。

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Windows.Forms;  
  5. using System.ComponentModel;  
  6. using System.Drawing;  
  7. namespace CustomControlSample  
  8. {  
  9.     public class FirstControl : Control  
  10.     {  
  11. private String _displayText=”Hello World!”;  
  12. private Color _textColor=Color.Red;  
  13.   public FirstControl()  
  14.         {  
  15.         }  
  16.         // ContentAlignment is an enumeration defined in the System.Drawing  
  17.         // namespace that specifies the alignment of content on a drawing   
  18.         // surface.  
  19.         private ContentAlignment alignmentValue = ContentAlignment.MiddleLeft;  
  20.         [  
  21.         Category("Alignment"),  
  22.         Description("Specifies the alignment of text.")  
  23.         ]  
  24.         public ContentAlignment TextAlignment  
  25.         {  
  26.             get 
  27.             {  
  28.                 return alignmentValue;  
  29.             }  
  30.             set 
  31.             {  
  32.                 alignmentValue = value;  
  33.                 // The Invalidate method invokes the OnPaint method described   
  34.                 // in step 3.  
  35.                 Invalidate();  
  36.             }  
  37.         }  
  38.  [Browsable(true)]  
  39.  [DefaultValue(“Hello World”)]  
  40.  public String DisplayText  
  41. {  
  42. get 
  43. {  
  44. return _displayText;  
  45. }  
  46. set 
  47. {  
  48.      _displayText =value;  
  49.     Invalidate();  
  50. }  
  51. }  
  52. [Browsable(true)]  
  53. public Color TextColor  
  54. {  
  55. get 
  56. {  
  57.     return _textColor;  
  58. }  
  59. set 
  60. {  
  61.     _textColor=value;  
  62. Invalidate();  
  63. }  
  64. }  
  65. public void ResetTextColor()  
  66. {  
  67.     TextColor=Color.Red;  
  68. }  
  69. public bool ShouldSerializeTextColor()  
  70. {  
  71. return TextColor!=Color.Red;  
  72. }  
  73. protected override void OnPaint(PaintEventArgs e)  
  74.         {  
  75.             base.OnPaint(e);  
  76.             StringFormat style = new StringFormat();  
  77.             style.Alignment = StringAlignment.Near;  
  78.             switch (alignmentValue)  
  79.             {  
  80.                 case ContentAlignment.MiddleLeft:  
  81.                     style.Alignment = StringAlignment.Near;  
  82.                     break;  
  83.                 case ContentAlignment.MiddleRight:  
  84.                     style.Alignment = StringAlignment.Far;  
  85.                     break;  
  86.                 case ContentAlignment.MiddleCenter:  
  87.                     style.Alignment = StringAlignment.Center;  
  88.                     break;  
  89.             }  
  90.             // Call the DrawString method of the System.Drawing class to write     
  91.             // text. Text and ClientRectangle are properties inherited from  
  92.             // Control.  
  93.             e.Graphics.DrawString(  
  94.                 DisplayText,  
  95.                 Font,  
  96.                 new SolidBrush(TextColor),  
  97.                 ClientRectangle, style);  
  98.         }  
  99.     }  

在上面C# WinForm控件開發(fā)的代碼中,我增加了兩個屬性,一個是DisplayText,這是一個簡單屬性,我們只需要在它的聲明前添加一個DefaultValue Attribute就可以了。

另外一個是TextColor屬性,這個復雜類型的屬性,所以我們提供了ResetTextColor和ShouldSerializeTextColor來實現(xiàn)默認值。

C# WinForm控件開發(fā)設置默認值的實現(xiàn)就講完了,但是有一點不要忽視了,你已經(jīng)設置默認值,就應該相應的初始化這些屬性,比如我們例子中的代碼:

 
 
 
  1. private String _displayText=”Hello World!”;  
  2. private Color _textColor=Color.Red; 

分享文章:詳解C#WinForm控件開發(fā)如何設置默認值
當前URL:http://m.5511xx.com/article/djhpcec.html