Dynamically Create Button in C#

Introduction

Below is an example demonstrating how to dynamically create buttons and add them to a panel in a Windows Forms application. Each button will have a name and value associated with it. On clicking any of these dynamically created buttons, a message box will display both the name and value of the clicked button:

using System;
using System.Windows.Forms;

namespace DynamicButtonPanelExample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            // Call the method to create buttons and add them to the panel
            CreateButtons();
        }

        private void CreateButtons()
        {
            // Number of buttons to create (for example, 5 buttons)
            int numberOfButtons = 5;

            // Loop to create buttons
            for (int i = 0; i < numberOfButtons; i++)
            {
                // Create a new Button instance
                Button newButton = new Button();

                // Set button properties
                newButton.Text = "Button " + (i + 1); // Button text
                newButton.Name = "btnDynamic" + i; // Button name
                newButton.Width = 100; // Button width
                newButton.Height = 30; // Button height

                // Attach a common event handler for button clicks
                newButton.Click += DynamicButtonClick;

                // Add the button to the panel
                panel1.Controls.Add(newButton);
            }
        }

        // Event handler for dynamically created buttons
        private void DynamicButtonClick(object sender, EventArgs e)
        {
            // Get the button that triggered the event
            Button clickedButton = sender as Button;

            // Display the name and value of the clicked button in a message box
            MessageBox.Show($"Button Name: {clickedButton.Name}\nButton Text: {clickedButton.Text}");
        }
    }
}

In this example, a Panel control (panel1) is used to contain the dynamically created buttons. The CreateButtons() method generates a specified number of buttons and sets their properties. The buttons are then added to the panel (panel1) using the Controls.Add() method. The DynamicButtonClick event handler is attached to each button and displays the name and text of the clicked button in a message box when clicked.

Modify the code as needed to fit your specific requirements for button names, values, or panel placement within your application.

using System;
using System.Windows.Forms;

namespace DynamicButtonPanelExample
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            // Call the method to create category buttons and add them to the panel
            CreateCategoryButtons();
        }

        private void CreateCategoryButtons()
        {
            // Sample category names
            string[] categoryNames = { "Category A", "Category B", "Category C", "Category D", "Category E" };

            // Loop to create category buttons
            for (int i = 0; i < categoryNames.Length; i++)
            {
                // Create a new Button instance
                Button categoryButton = new Button();

                // Set button properties
                categoryButton.Text = categoryNames[i]; // Button text (category name)
                categoryButton.Name = "btnCategory" + i; // Button name
                categoryButton.Width = 120; // Button width
                categoryButton.Height = 30; // Button height
                categoryButton.FlatStyle = FlatStyle.Flat; // Set flat style
                categoryButton.FlatAppearance.BorderColor = SystemColors.ControlDarkDark; // Set border color

                // Attach a common event handler for category button clicks
                categoryButton.Click += CategoryButtonClick;

                // Add the category button to the panel
                panelCategories.Controls.Add(categoryButton);
            }
        }

        // Event handler for category buttons
        private void CategoryButtonClick(object sender, EventArgs e)
        {
            // Get the category button that triggered the event
            Button clickedCategoryButton = sender as Button;

            // Display the name of the clicked category button in a message box
            MessageBox.Show($"Clicked Category: {clickedCategoryButton.Text}");
        }
    }
}

This code dynamically creates category buttons (btnCategory) based on an array of sample category names. Each button is added to the panelCategories. Upon clicking any of these category buttons, a message box displays the name of the clicked category.


Similar Articles