Baruch
Baruch

Reputation: 21508

Substituting custom control for existing control

I have a WinForms form with a few text boxes on it. I need to change some of them to a custom version of TextBox (I had to override a function). Since the whole new control is just this one function, I just did it all in a few lines of code in same as a contained-class in my form. Now I want to change the instance of the designer-generated text box to an instance of this one in the code. Can this be done somehow?

If it can't, and I have to add a whole new UserControl item to my project, can I at least substitute it in the designer without losing all my property changes and event handler bindings?

Upvotes: 3

Views: 98

Answers (2)

S2S2
S2S2

Reputation: 8502

Go to the InitializeComponent() method inside your form class or the form's designer.cs class. In this method, you would find the usage of your textbox controls, now go to the definition of those usages and change the type of the textbox variables to your custom implemented textbox types.

Upvotes: 1

dknaack
dknaack

Reputation: 60448

Description

Check out the <YourFormName>.Designer.cs file. This is the file the Designer has created. You can change your TextBox to your custom this way.

Sample

#region Windows Form Designer generated code
private void InitializeComponent()
{
    // change this
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(0, 0);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(100, 20);
    this.textBox1.TabIndex = 0;
    // 
    // Form1
    // 
    // ...

}

#endregion

// change this 
private System.Windows.Forms.TextBox textBox1; 

Upvotes: 3

Related Questions