C# – How to create Button control dynamically?

C# allows us to create the controls dynamically, and place them on Windows Forms. Through this article, we are going to create a Button control dynamically. I recommend you to read “C# – How to use Button control?” article, before continue to read this Article.

Create a Button

Step 1. Button class is used to create Button control. Our first step is, to create an instance of Button class. Assuming you already created the Windows Forms project in Visual Studio IDE. 🙂

Button dynamicButton = new Button();

Step 2. We need to display the text on the Button and the control should be visible on the Form. Below properties of Button control, will set this for us.

dynamicButton.Text = "Dynamic";
dynamicButton.Visible = true;

Step 3. Now, we have our Button control created dynamically. Let’s see whether we see it, when we RUN our Program. Run the program, and observe that, our Button control is not visible on Windows Form. Why?

This is because, we need to attach our control to the Form to display on it. Our control was created successfully; it is in the memory; but, to display it on the Windows Form, we need to add our control to the Form.

Attach Button to the Form

Step 4. Form class represents Windows Form. Whenever we place or add a control to the Form; it maintains a collection to keep the controls and using this collection, it displays all the controls on it’s display area. Hence, we see the controls on the Form.

Now, we need to add our control to Form’s collection. As mentioned above, Form class represents Windows Form; and it has Controls property to holds the list of controls attached to the Form. We use this property to add our control to the Form. Below code will do this; and this has to be in our Form’s class. Remember that, our Form’s class (example; Form1) is derived from Form class.


Step 5. Now, again RUN the program; observe that, our Button control was displayed on the Form. But, it was displayed at (0, 0) location on the Form. We can place the Button, in the specified position on the Form, by using it’s Location property.

buttonDynamic.Location = new Point(12, 12);

Add an event handler

Step 6. After running the program, once Application window is opened, Click on our Button control. You see nothing was happened. Right? Because, we need to add an event handler to it. Event handlers execute when an Event occurred.

Button control will generate Click event, when we click on it. As it doesn’t have any event handler attached to it; we do not see anything happened, when we click on it.

We need to create our event handler, and attach it, to the Click event of the Button control. += operator is used to attach the event handler to the Click event. When we use += operator, Visual Studio will automatically gives us the option to create an event handler for us.

If your Visual Studio version is not showing the option to create the event handler; you can use the below code. Remember that, this method must match the signature of EventHandler delegate.

buttonDynamic.Click += ButtonDynamic_Click;

And the event handler code is;

private void ButtonDynamic_Click(object sender, EventArgs e)
    throw new NotImplementedException();

Observe that, inside the event handler code, Visual Studio added code to throw an exception. Because, it is to ensure to replace this code with our own code.

Replace the throw statement with below code; to display a message box when we click on the Button control.

MessageBox.Show("Click event handler!", "Message Box", MessageBoxButtons.OK);

Step 7. Now, RUN the program and observe that when we click on the Button control, it will display the Message Box.

Complete working code

Here is the complete code; this shows a Windows Form with “Create” button on it. Once click on the “Create” button, it will add a “Dynamic” button at the location (12, 12) on the Form. Also observe that, “Create” button will be disabled, once we click on it to avoid to create multiple dynamic button controls. When click on the “Dynamic” button, it will display the message box.

Screenshot of the Application window is looks like below;

Dynamically created Button control
Dynamically created Button control

We will discuss more topics in our upcoming Articles. Do not forget to give your feedback through the below Comments section.


One Comment

Add a Comment

Your email address will not be published.