EgyEast
EgyEast

Reputation: 1572

What is the best way to create customized Windows Form controls?

I'm making C# Windows Form application that has many forms that use many textboxes and labels of the same properties and style. Instead of changing properties of every textbox and every label I created class called MyTextBox that inherits from System.Windows.Forms.TextBox and then changed its properties in class constructor like this:

class MyTextBox:TextBox
{
    public MyTextBox()
    {
        this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.Font = new System.Drawing.Font("Bookman Old Style", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.ForeColor = System.Drawing.Color.Blue;
        this.Size = new System.Drawing.Size(257, 23);
    }
}

After building project class appeared in toolbox and by making instances from this class on my form it worked fine. The problem is that when I change any of the properties in MyTextBox class and rebuilding project, changes do not apply to the already instanced objects and when I looked at the designer code, I found that the IDE copied all properties from MyTextBox class to the designer code so I have to recreate all my instances after any change to class code.

private void InitializeComponent()
{
    this.MyTextBox1 = new WindowsFormsApplication9.MyTextBox();
    this.SuspendLayout();
    // 
    // MyTextBox1
    // 
    this.MyTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    this.MyTextBox1.Font = new System.Drawing.Font("Bookman Old Style", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.MyTextBox1.ForeColor = System.Drawing.Color.Blue;
    this.MyTextBox1.Location = new System.Drawing.Point(67, 43);

Any way to solve this problem? I want any changes to the class code applied to all already instanced objects without need to recreate them or if there is a better way please help.

Suppose I need to be controlling 5 properties of MyTextBox instance like ForeColor,default Width, default Font Style , default Font size and BorderStyle. All of them except Width property are not supposed to have other value than default value.

Upvotes: 2

Views: 245

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125197

First you should provide suitable default values for properties in constructor. Then you should override or shadow properties and decorate them with one of these attributes:

Code

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public partial class MyTextBox : TextBox
{
    public MyTextBox()
    {
        this.ForeColor = Color.Red;
        this.Font = new Font("Tahoma", 9, FontStyle.Italic);
        this.Width = 200;
    }

    [DefaultValue(typeof(Color), "Red")]
    public override Color ForeColor
    {
        get { return base.ForeColor; }
        set { base.ForeColor = value; }
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override Font Font
    {
        get { return base.Font; }
        set { base.Font = value; }
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public new Size Size
    {
        get { return base.Size; }
        set { base.Size = value; }
    }
}

In the above example, I created a custom TextBox having these features:

  • The default value for ForeColor is Color.Red. If you change value of ForeColor in instances of the control, the value will be serialized. If you change the default value in class, only those instances which their ForeColor was untouched will use new default value, other instances will use their ForeColor value.
  • The default value for Font is new Font("Tahoma", 9, FontStyle.Italic) and since we said to the designer to not serialize Font property, the new value for property will not be saved if you change the value of different instances and all instances will use default value which is set in constructor of MyTextBox.
  • For setting default Width which user can not change it using designer, I overrided Size and said the designer to not serialize it, so the width will be set to the default Width which I set in constructor.

Upvotes: 4

WarrenG
WarrenG

Reputation: 3084

A settings file should do what you want. Create a new settings file in your project, if one doesn't exist already, and add a new setting like this:

enter image description here

then in your custom TextBox override OnCreateControl

protected override void OnCreateControl() {
    base.OnCreateControl();
    ForeColor = Settings.Default.TextBox_ForeColor;
}

Now you can change ForeColor in the settings file and the changes will cascade to all instances of your custom TextBox. Follow the same pattern for Font, Size, etc, just be sure to set the correct Type in the settings file.

Upvotes: 0

Related Questions