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

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

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

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

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

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

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

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

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

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

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

 
 
 
  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ā)的代碼中,我增加了兩個(gè)屬性,一個(gè)是DisplayText,這是一個(gè)簡(jiǎn)單屬性,我們只需要在它的聲明前添加一個(gè)DefaultValue Attribute就可以了。

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

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

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

網(wǎng)站欄目:詳解C#WinForm控件開發(fā)如何設(shè)置默認(rèn)值
文章網(wǎng)址:http://m.5511xx.com/article/djhpcec.html