自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解C# WinForm控件開發(fā)如何設(shè)置默認(rèn)值

開發(fā) 后端
我要講一下C# WinForm控件開發(fā)屬性的設(shè)置默認(rèn)值。如果我們希望自己C# WinForm控件開發(fā)更易于被其它開發(fā)者使用,那么提供默認(rèn)值是非常值得的。

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

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

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

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

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

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

VS能夠根據(jù)方法的名稱來識別這種方法,比如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ā)的代碼中,我增加了兩個屬性,一個是DisplayText,這是一個簡單屬性,我們只需要在它的聲明前添加一個DefaultValue Attribute就可以了。

另外一個是TextColor屬性,這個復(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; 

原文標(biāo)題:WinForm控件開發(fā)總結(jié)(十)-----為屬性設(shè)置默認(rèn)值

鏈接:http://www.cnblogs.com/guanjinke/archive/2006/12/24.html

【編輯推薦】

  1. 詳解TripleDES實(shí)現(xiàn)C# 加密操作
  2. 淺析C# WinForm控件開發(fā)前期準(zhǔn)備
  3. 詳解C# WinForm自定義控件的使用和調(diào)試
  4. C# Attribute的概念與使用淺析
  5. C# AttributeUsage的使用淺析
責(zé)任編輯:阡陌 來源: 博客園
相關(guān)推薦

2021-02-25 13:40:17

MySQL數(shù)據(jù)庫默認(rèn)值

2009-09-11 10:41:20

C# WinForm控

2009-10-10 14:54:44

treeView1控件

2009-08-20 09:30:03

C#開發(fā)WinForm

2009-08-20 10:24:52

C#開發(fā)WinForm

2009-09-11 11:33:58

C# WinForm控Attribute

2009-09-11 12:07:12

C# WinForm控

2009-09-11 11:04:23

C# WinForm自

2009-08-24 11:23:41

C# TimeLabe

2009-11-20 09:10:21

C#開發(fā)環(huán)境

2009-09-07 03:58:42

WinForm傳值

2009-09-01 10:35:59

C# WinForm控

2009-08-24 10:10:09

C#復(fù)合控件

2009-09-11 12:52:09

C# WinForm控編輯器

2009-09-16 10:56:22

C#開發(fā)ActiveX

2009-04-01 16:26:06

LabelWinFormC#

2010-11-23 16:49:42

MySQL設(shè)置當(dāng)前時間

2010-09-28 10:35:58

SQL字段默認(rèn)值

2010-09-07 16:05:23

SQL語句刪除

2010-09-28 10:23:36

SQL修改字段
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號