Naveen

Naveen

  • 1.5k
  • 147
  • 8.8k

Adding dynamic buttons when i click a button

Jun 24 2024 10:39 AM

using DevExpress.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ButtonApp
{
    public partial class Form1 : Form
    {
        private FlyoutPanel flyoutPanelButtons;
        private Button dynamicButton;
       // private Button simpleButton1;
        public Form1()
        {
            InitializeComponent();
            InitializeFlyoutPanel();
        }

        private void InitializeFlyoutPanel()
        {
            flyoutPanelButtons = new FlyoutPanel
            {
                Location = new Point(10, 50), // Set the desired location
                Size = new Size(400, 300), // Set the desired size
                //Options = { ButtonPanel = { ShowButtonPanel = false } }, // Configure options as needed
                Parent = this // Set the parent to this form
            };
            Controls.Add(flyoutPanelButtons);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //Clear any existing buttons in the FlowLayoutPanel
            flowLayoutPanelButtons.Controls.Clear();

            // Get the number of buttons to generate
            if (int.TryParse(textBox1.Text, out int numberOfButtons))
            {
                for (int i = 0; i < numberOfButtons; i++)
                {
                    // Create a new button
                    dynamicButton = new Button();
                    dynamicButton.Text = $"Button {i + 1}";
                    dynamicButton.Width = 100; // Set width as needed
                    dynamicButton.Height = 30; // Set height as needed
                    //dynamicButton.Margin = new Padding(5); // Set margin as needed
                    dynamicButton.Location = flyoutPanelButtons.Location;

                    // Optionally, add a Click event handler for the dynamic button
                    //dynamicButton.Click += DynamicButton_Click;

                    // Add the button to the FlowLayoutPanel
                    this.Controls.Add(dynamicButton);
                    flyoutPanelButtons.Refresh();
                }
            }
            else
            {
                MessageBox.Show("Please enter a valid number.");
            }
            //List<Button> buttons = new List<Button>();
            //for (int i = 0; i < 10; i++)
            //{
            //    Button newButton = new Button();
            //    buttons.Add(newButton);
            //    this.Controls.Add(newButton);
            //}
        }

        private void DynamicButton_Click(object sender, EventArgs e)
        {
            Button clickedButton = sender as Button;
            MessageBox.Show($"You clicked {clickedButton.Text}");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
How to create dnamic buttons when i click a button. here when i click a button it is showing only 1 button but i need more than 1


Answers (1)