详解C# WinForm控件开发如何设置默认值

开发 后端
我要讲一下C# WinForm控件开发属性的设置默认值。如果我们希望自己C# WinForm控件开发更易于被其它开发者使用,那么提供默认值是非常值得的。

C# WinForm控件开发设置默认值是非常有必要的,实现起来也很容易,本文笔者为你介绍设置默认值的方法,希望能给你带来帮助。

如果你为属性设定了默认值,那么当开发者修改了属性的值,这个值在Property Explorer中将会以粗体显示。VS为属性提供一个上下文菜单,允许程序员使用C# WinForm控件开发把值重置为默认值。

当Visual Studio进行控件的串行化时,他会判断那些值不是默认值,只有不是设置默认值的属性才会被串行化,所以为属性提供设置默认值时可以大大减少串行化的属性数目,提高效率。

那么Visual Studio进怎么知道我们的属性值不是默认值了呢?我们需要一种机制来通知Visual Studio进默认值。实现这种机制有两种方法:

对于简单类型的属性,比如Int32,Boolean等等这些Primitive类型,你可以在属性的声明前设置一个DefaultValueAttribute,在Attribute的构造函数里传入设置默认值。

对于复杂的类型,比如Font,Color,你不能够直接将这些类型的值传递给Attibute的构造函数。相反你应该提供Reset 和ShouldSerialize方法,比如ResetBackgroundColor(),ShouldSerializeBackgroundColor()。

VS能够根据方法的名称来识别这种方法,比如Reset方法把重置为设置默认值,ShouldSerialize方法检查属性是否是设置默认值。过去我们把它称之为魔术命名法,应该说是一种不好的编程习惯,可是现在微软依然使用这种机制。我还是以前面几篇文章使用的例子代码。

  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控件开发的代码中,我增加了两个属性,一个是DisplayText,这是一个简单属性,我们只需要在它的声明前添加一个DefaultValue Attribute就可以了。

另外一个是TextColor属性,这个复杂类型的属性,所以我们提供了ResetTextColor和ShouldSerializeTextColor来实现默认值。

C# WinForm控件开发设置默认值的实现就讲完了,但是有一点不要忽视了,你已经设置默认值,就应该相应的初始化这些属性,比如我们例子中的代码:

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

原文标题:WinForm控件开发总结(十)-----为属性设置默认值

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

【编辑推荐】

  1. 详解TripleDES实现C# 加密操作
  2. 浅析C# WinForm控件开发前期准备
  3. 详解C# WinForm自定义控件的使用和调试
  4. C# Attribute的概念与使用浅析
  5. C# AttributeUsage的使用浅析
责任编辑:阡陌 来源: 博客园
相关推荐

2021-02-25 13:40:17

MySQL数据库默认值

2009-09-11 10:41:20

C# WinForm控

2009-10-10 14:54:44

treeView1控件

2009-08-20 09:30:03

C#开发WinForm

2009-08-20 10:24:52

C#开发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#开发环境

2009-09-07 03:58:42

WinForm传值

2009-09-01 10:35:59

C# WinForm控

2009-09-11 12:52:09

C# WinForm控编辑器

2009-08-24 10:10:09

C#复合控件

2010-11-23 16:49:42

MySQL设置当前时间

2010-09-28 10:35:58

SQL字段默认值

2010-09-07 16:05:23

SQL语句删除

2010-09-28 10:23:36

SQL修改字段

2009-12-18 17:07:14

2009-09-16 10:56:22

C#开发ActiveX
点赞
收藏

51CTO技术栈公众号