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

长沙县网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。成都创新互联于2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联。
如果你为属性设定了默认值,那么当开发者修改了属性的值,这个值在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 方法检查属性是否是设置默认值。过去我们把它称之为魔术命名法,应该说是一种不好的编程习惯,可是现在微软依然使用这种机制。我还是以前面几篇文章使用的例子代码。
- using System;
 - using System.Collections.Generic;
 - using System.Text;
 - using System.Windows.Forms;
 - using System.ComponentModel;
 - using System.Drawing;
 - namespace CustomControlSample
 - {
 - public class FirstControl : Control
 - {
 - private String _displayText=”Hello World!”;
 - private Color _textColor=Color.Red;
 - public FirstControl()
 - {
 - }
 - // ContentAlignment is an enumeration defined in the System.Drawing
 - // namespace that specifies the alignment of content on a drawing
 - // surface.
 - private ContentAlignment alignmentValue = ContentAlignment.MiddleLeft;
 - [
 - Category("Alignment"),
 - Description("Specifies the alignment of text.")
 - ]
 - public ContentAlignment TextAlignment
 - {
 - get
 - {
 - return alignmentValue;
 - }
 - set
 - {
 - alignmentValue = value;
 - // The Invalidate method invokes the OnPaint method described
 - // in step 3.
 - Invalidate();
 - }
 - }
 - [Browsable(true)]
 - [DefaultValue(“Hello World”)]
 - public String DisplayText
 - {
 - get
 - {
 - return _displayText;
 - }
 - set
 - {
 - _displayText =value;
 - Invalidate();
 - }
 - }
 - [Browsable(true)]
 - public Color TextColor
 - {
 - get
 - {
 - return _textColor;
 - }
 - set
 - {
 - _textColor=value;
 - Invalidate();
 - }
 - }
 - public void ResetTextColor()
 - {
 - TextColor=Color.Red;
 - }
 - public bool ShouldSerializeTextColor()
 - {
 - return TextColor!=Color.Red;
 - }
 - protected override void OnPaint(PaintEventArgs e)
 - {
 - base.OnPaint(e);
 - StringFormat style = new StringFormat();
 - style.Alignment = StringAlignment.Near;
 - switch (alignmentValue)
 - {
 - case ContentAlignment.MiddleLeft:
 - style.Alignment = StringAlignment.Near;
 - break;
 - case ContentAlignment.MiddleRight:
 - style.Alignment = StringAlignment.Far;
 - break;
 - case ContentAlignment.MiddleCenter:
 - style.Alignment = StringAlignment.Center;
 - break;
 - }
 - // Call the DrawString method of the System.Drawing class to write
 - // text. Text and ClientRectangle are properties inherited from
 - // Control.
 - e.Graphics.DrawString(
 - DisplayText,
 - Font,
 - new SolidBrush(TextColor),
 - ClientRectangle, style);
 - }
 - }
 - }
 
在上面C# WinForm控件开发的代码中,我增加了两个属性,一个是DisplayText,这是一个简单属性,我们只需要在它的声明前添加一个DefaultValue Attribute就可以了。
另外一个是TextColor属性,这个复杂类型的属性,所以我们提供了ResetTextColor和ShouldSerializeTextColor来实现默认值。
C# WinForm控件开发设置默认值的实现就讲完了,但是有一点不要忽视了,你已经设置默认值,就应该相应的初始化这些属性,比如我们例子中的代码:
- private String _displayText=”Hello World!”;
 - private Color _textColor=Color.Red;
 
                网页题目:详解C#WinForm控件开发如何设置默认值
                
                网页网址:http://www.csdahua.cn/qtweb/news10/544060.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网