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

Source: Internet
Author: User
Tags constructor reset

The previous articles in this series explain how to define attributes and more efficient editing properties, and then I'll talk about the default values for control properties. If we want to develop our own controls that are easier for other developers to use, it is worthwhile to provide a default value.

If you set a default value for a property, then when the developer modifies the value of the attribute, the value is shown in bold in the property explorer. VS provides a context menu for attributes that allows programmers to use controls to reset values to their default values. When vs is serialized for a control, he determines that those values are not the default values and that only attributes that are not default are serialized, so providing a default value for a property can greatly reduce the number of serialized attributes and increase efficiency.

So vs how do we 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 types of properties, such as Int32,boolean and so on, you can set a defaultvalueattribute before the declaration of the attribute, and pass in the default value in the constructor.

For complex types, such as Font,color, you cannot directly pass values of these types to the Attibute constructor. Instead you should provide reset<propertyname> and shouldserialize<propertyname> methods, such as Resetbackgroundcolor (), Shouldserializebackgroundcolor (). VS can recognize this method based on the name of the method, such as the Reset<propertyname> method, which resets the default value,shouldserialize<propertyname> method to check whether the property is a default value. We used to call it the Magic Nomenclature, which is a bad programming habit, but now Microsoft still uses this mechanism. I still use the example code in the 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 be an enumeration defined in the System.Drawing

        Namespace that specifies the alignment's content on a drawing//surface.

        Private ContentAlignment alignmentvalue = Contentalignment.middleleft;

        [Category ("alignment"), Description ("Specifies the alignment of text.") ] public ContentAlignment TextAlignment {get {return ALIGNM

            Entvalue;

                } set {alignmentvalue = value; The Invalidate method invokes theOnPaint method described//in step 3.

            Invalidate (); } [Browsable (True)] [DefaultValue (' Hello World ')] public String DisplayText {get {return _displayt

Ext

    } 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, one is displaytext, this is a simple attribute, we just need to add a DefaultValue attribute before its declaration. The other is the TextColor attribute, which is a complex type of property, so we provide resettextcolor and Shouldserializetextcolor to implement the default values.

The implementation of the default is finished, but let's not overlook the fact that you set the default value and you should initialize the 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.