Add Controls At Runtime & Also Perform An Event Of That Control


In this article I show you how to add the two controls "Textbox" & "Button" at runtime.

So first of all we will see how you can create an objects of those controls:

-> TextBox mytxt = new TextBox();

-> Button mybtn = new Button();

Using the above two lines it will create an object for "Textbox" & "Button" respectively.

After that we will add those controls into the form using the code shown below:
-> this.Controls.Add(mytxt);

-> this.Controls.Add(mybtn);

That will add "mytxt" & "mybtn" to your form.

After that you must set the location of the controls on the form:

-> mytxt.Location=new Point(10, 10);

-> mybtn.Location = new Point(10, 40);

Here you can set your own location which you want to show on the form.

Here Textbox will display at X-coordinate & Y-coordinate as 10.

And or button X-coordinate as 10 & Y-coordinate as 40.

After that set the value which will display on button.

-> mybtn.Text = "My Custom Button";

The above code is the basic part; now the main thing of this article is how you can add events of new added controls? Right?

-> mytxt.MouseHover += new EventHandler(mytxt_MouseHover);

-> mybtn.Click += new EventHandler(mybtn_Click);

Here I add one event for Textbox & another event for Button.

I add "MouseHover" event for the Texbox & "Click" event for the Button, but you can add your event from the list…

You can add another event for the list as shown in the image below:

TextBox1.JPG

One more thing to keep in mind is that you can write your own event method name instead of "mytxt_MouseHover" and "mybtn_Click", but it is a good practice to use the name as I use.

After that you can define your event like shown below:

void mytxt_MouseHover(object sender, EventArgs e)
{
//write here your logic for Textbox MouseHover Event…
}


Another thing is that whatever you use for your event method name, that name must match the method name used in the definition.

See the following image for a clearer understanding:

TextBox2.JPG

Main code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace AddControlAndItsEventsAtRuntime
{
    public partial class Form1 : Form
    {
      public Form1()
        {
            InitializeComponent();
           
//create new object of the textbox...
            TextBox mytxt = new TextBox();
           
//add new custom textbox to form...
            this.Controls.Add(mytxt);
           
//Set the location of textbox on to form...

            mytxt.Location=new Point(10, 10);
           
//Add new Mouse Hover event for the textbox...
            mytxt.MouseHover += new EventHandler(mytxt_MouseHover);
           
 
           
//create new object of the Button...
            Button mybtn = new Button();
           
//add new custom Button to form...
            this.Controls.Add(mybtn);
           
//Set the location of Button on to form...
            mybtn.Location = new Point(10, 30);
           
//Set Text To Button....
            mybtn.Text = "My Custom Button";

            //Add new click event for the Button...
            mybtn.Click += new EventHandler(mybtn_Click);
        }

        //New added textbox Mouse Hover evnet function definition...
        void mytxt_MouseHover(object sender, EventArgs e)
        {
            MessageBox.Show("mytxt Mouse Hover Event....");
        }

        //New added Button Click evnet function definition...
        void mybtn_Click(Object sender, EventArgs e)
        {
            MessageBox.Show("mybtn Click Event....");
        }
    }
}


The output of that is like below :

-> On MouseHover Event it will change the background color of the form to "LightGreen"...

TextBox3.JPG

-> on button "Click" event it will display MessageBox...

TextBox4.JPG

I hope this article is helpful for coders...

Up Next
    Ebook Download
    View all
    Learn
    View all