WinForm Control Development Summary (10) -- Set default values for properties

Source: Internet
Author: User
The previous articles in this series explain how to define attributes and edit attributes more effectively. Next I will talk about the default values of control attributes. If we want to make the self-developed controls easier to use by other developers, it is worthwhile to provide default values.
If you set the default value for the Property, when the developer modifies the Property value, this value will be displayed in bold in Property Explorer. VS provides a context menu for properties, allowing programmers to reset the value to the default value using controls. When VS serializes controls, it determines that those values are not default values, and only those attributes that are not default values will be serialized, therefore, when default values are provided for attributes, the number of serialized attributes can be greatly reduced to improve efficiency.
So how does VS know that our attribute value is not the default value? We need a mechanism to notify VS default values. There are two ways to implement this mechanism:
For simple type attributes, such as Int32 and Boolean, you can set a DefaultValueAttribute before the Attribute declaration and input the default value in the Attribute constructor.
For complex types, such as Font and Color, you cannot directly pass these types of values to the attivity constructor. Instead, you should provide the Reset <PropertyName> and ShouldSerialize <PropertyName> methods, such as ResetBackgroundColor () and ShouldSerializeBackgroundColor (). VS can identify this method based on the method name. For example, the Reset <PropertyName> method resets to the default value, and the ShouldSerialize <PropertyName> method checks whether the attribute is the default value. In the past, we called it the magic naming method. It should be said that it is a bad programming habit, but Microsoft still uses this mechanism. I still use the sample code from several previous articles.

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 );

}
}
}

In the above code, I added two attributes: DisplayText, which is a simple Attribute. We only need to add a DefaultValue Attribute before its declaration. The other is the TextColor attribute, which is a complex type attribute. Therefore, we provide ResetTextColor and ShouldSerializeTextColor to implement the default value.
The implementation of the default value is finished, but do not ignore it. If you set the default value, you should initialize these attributes accordingly, such as the Code in our example:

Private String _ displayText = "Hello World !";
Private Color _ textColor = Color. Red;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.