Creating a Button at Run-time in C#



Creating a Button Dynamically

Creating a Button control at run-time is merely a work of creating an instance of Button class, set its properties and add Button class to the Form controls.

The first step is to create an instance of Button class. The following code snippet creates a Button control object.

// Create a Button object 
Button dynamicButton = new Button();

Next step, you need to set Button class properties.  You need to make sure to specify the Location, Width, Height or Size properties. The default location of Button is left top corner of the Form. The Location property takes a Point that specifies the starting position of the Button on a Form. The Size property specifies the size of the control. We can also use Width and Height property instead of Size property. The following code snippet sets Location, Width, and Height properties of a Button control.

// Set Button properties
dynamicButton.Location = new Point(20, 150);
dynamicButton.Height = 40;
dynamicButton.Width = 300;



In the next step, you may set more properties of the Button control. The following code snippet sets background color, foreground color, Text, Name, and Font properties of a Button.

// Set background and foreground
dynamicButton.BackColor = Color.Red;
dynamicButton.ForeColor = Color.Blue;
         
dynamicButton.Text = "I am Dynamic Button";
dynamicButton.Name = "DynamicButton";
dynamicButton.Font = new Font("Georgia", 16);


A Button control is used to process the button click event. We can attach a button click event handler at run-time by setting its Click event to an EventHandler obect. The EventHandler takes a parameter of an event handler. The Click event is attached in the following code snippet.

// Add a Button Click Event handler
dynamicButton.Click += new EventHandler(DynamicButton_Click);

The signature of Button click event handler is listed in the following code snippet.

private void DynamicButton_Click(object sender, EventArgs e)
{ }

Now the last step is adding a Button control to the Form. The Form.Controls.Add method is used to add a control to a Form. The following code snippet adds a Button control to the current Form.

Controls.Add(dynamicButton);  

The complete code is listed in Listing 1, where CreateDynamicButton methods creates a Button control to a Form at run-time, attaches a click event handler of the button and adds Button control to the Form by calling Form.Controls.Add() method.

/// <summary>

/// This method creates a Button control at runtime

/// </summary>

private void CreateDynamicButton()

{

    // Create a Button object

    Button dynamicButton = new Button();

 

    // Set Button properties

    dynamicButton.Height = 40;

    dynamicButton.Width = 300;

    dynamicButton.BackColor = Color.Red;

    dynamicButton.ForeColor = Color.Blue;

    dynamicButton.Location = new Point(20, 150);

    dynamicButton.Text = "I am Dynamic Button";

    dynamicButton.Name = "DynamicButton";

    dynamicButton.Font = new Font("Georgia", 16);

           

    // Add a Button Click Event handler

    dynamicButton.Click += new EventHandler(DynamicButton_Click);

 

    // Add Button to the Form. Placement of the Button

    // will be based on the Location and Size of button

    Controls.Add(dynamicButton);           

}

 

/// <summary>

/// Button click event handler

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void DynamicButton_Click(object sender, EventArgs e)

{

    MessageBox.Show("Dynamic button is clicked");

}



Listing 1


You need to make sure to call CreateDynamicButton() method on the Form's constructor just after InitializeComponent() method, listed as following.

public Form1()

{

    InitializeComponent();

    CreateDynamicButton();

}


Summary


In this article, we discussed how to create a Windows Forms Button controly, set its properties and add a click event handler at run-time.


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.