铁心桥钟山驾校地址:WinForm控件开发总结(十)
来源:百度文库 编辑:九乡新闻网 时间:2024/05/02 15:24:57
WinForm控件开发总结(十)-为属性设置默认值
2009-07-15 来自:博客园 字体大小:【大 中 小】- 摘要:本文介绍如何为自定义控件设置默认值的相关知识。
如果你为属性设定了默认值,那么当开发者修改了属性的值,这个值在Property Explorer中将会以粗体显示。VS为属性提供一个上下文菜单,允许程序员使用控件把值重置为默认值。当VS进行控件的串行化时,他会判断那些值不是默认值,只有不是默认值的属性才会被串行化,所以为属性提供默认值时可以大大减少串行化的属性数目,提高效率。
那么VS怎么知道我们的属性值不是默认值了呢?我们需要一种机制来通知VS默认值。实现这种机制有两种方法:
对于简单类型的属性,比如Int32,Boolean等等这些Primitive类型,你可以在属性的声明前设置一个DefaultValueAttribute,在Attribute的构造函数里传入默认值。
对于复杂的类型,比如Font,Color,你不能够直接将这些类型的值传递给Attibute的构造函数。相反你应该提供Reset
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);
}
}
}
在上面的代码中,我增加了两个属性,一个是DisplayText,这是一个简单属性,我们只需要在它的声明前添加一个DefaultValue Attribute就可以了。另外一个是TextColor属性,这个复杂类型的属性,所以我们提供了ResetTextColor和ShouldSerializeTextColor来实现默认值。
默认值的实现就讲完了,但是有一点不要忽视了,你设定了默认值,就应该相应的初始化这些属性,比如我们例子中的代码:
private Color _textColor=Color.Red;
作者:纶巾客
WinForm控件开发总结(十)
WinForm控件开发总结(二)
WinForm控件开发总结(四)
WinForm控件开发总结(八)
WinForm控件开发总结(十一)
c# WinForm开发 DataGridView控件的各种操作总结
C# WinForm开发系列 - 介绍一些开源的第三方控件
C# WinForm 给某动态控件设置 气泡状提示
我dotNET 所以我绚丽 (WinForm控件小选)
使用C#开发ActiveX控件
ASP.NET 控件开发速成教程:生成复合控件
ASP.NET 2.0服务器控件开发之控件样式
MFC控件使用总结——CListCtrl
XML开发笔记 总结
如何开发web带参数的自定义控件
开发ActiveX控件使用MFC和ATL之比较
CodeSmith开发系列资料总结
usb驱动程序开发技术总结
重温delphi之:如何快速开发原生ActiveX控件 - 菩提树下的杨过.Net - 博...
symbian开发总结 - 移动平台 / Symbian
手机网站开发必修课[2009总结版]
十大开发代码原则
十大开发代码原则
房地产项目开发流程十阶段