[C#] How to create button programatically

Discussion in 'Mixed Languages' started by carmz, Aug 14, 2012.

  1. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    311
    10
    10
    #1 carmz, Aug 14, 2012
    Last edited by a moderator: Apr 20, 2017
    Hi,can i ask some help, i want to create buttons and add this to the Panel..

    I want that when i am going to click the start button this will display 12 buttons in my Panel,

    Here is my code...
    Code:
    namespace RandomNumbers
    {
        public partial class Form1 : Form
        {
            Button btn;
            Random ran;
            Button b;
            public Form1()
            {
                InitializeComponent();
                
              //  pnl.BorderStyle = BorderStyle.Fixed3D;
    
            }
    
          
    
            private void button1_Click(object sender, EventArgs e)
            {
               
    
                    FlowLayoutPanel flp = new FlowLayoutPanel();
                    flp.BorderStyle = BorderStyle.Fixed3D;
                    flp.Size = new Size(470, 160);
    
                
                    pnl.BorderStyle = BorderStyle.Fixed3D;
                    flp.FlowDirection = FlowDirection.TopDown;
                    flp.Controls.Add(createButton_click);
                  
                 
                    pnl.Controls.Add(flp);
               
    
            }
            private void button2_Click(object sender, EventArgs e)
            {
                Close();
            }
    
            private void createButton_click(object sender, EventArgs e)
            {
                Button bb = new Button();
                bb.Text = "?";
    
            }
        }
    }
    
    
    
    
    
    
     
  2. wallace87000

    wallace87000 MDL Novice

    Jan 9, 2010
    6
    3
    0
    #2 wallace87000, Aug 24, 2012
    Last edited by a moderator: Apr 20, 2017
    Hi Carmz,

    Just a little reflexion about creating buttons dynamically :

    1- Which button properties are required ?

    - Name (unique name)
    - Location
    - Size

    2- Where do you want to add these buttons ?

    - Panel (because when the number exceeds the panel's size scrollbars appear)

    3- Which Event Handler would you use ?

    - Click


    OK let's do it :

    FIRST : Add a panel (Pnl) and a button (createButton) in your main Form.

    Code:
    private void createButton_click(object sender, EventArgs e)
    {
    // How many buttons do you want ?
    int NumOfButtons = 12;
    // X Location of each created button in the panel
    int loc = 20;
    for (int i = 1; i <= NumOfButtons; i++) {
    Button btn = new Button();
    {
    btn.Name = "Btn-" + i.tostring;
    btn.Size = new Size(50, 20);
    btn.Tag = i;
    btn.Text = "Browse-" + i.tostring;
    btn.location = new Point(5, loc);
    }
    // Add Click event Handler for each created button
    btn.Click += Buttons_Click;
    loc += 20;
    // Add the created btn to panl
    pnl.Controls.Add(btn);
    }
    }
    
    private void Buttons_Click(System.Object sender, System.EventArgs e)
    {
    // Use "Sender" to know which button was clicked ?
    Button btn = sender as Button;
    Interaction.Msgbox("Name : " + btn.name + Constants.vbnewline + "Tag : " + Btn.Tag);
    }
     
    This is not optimize but I hope it will help you to keep on the right way ;)
     
  3. carmz

    carmz MDL Senior Member

    Nov 29, 2011
    311
    10
    10
    #3 carmz, Aug 29, 2012
    Last edited by a moderator: Apr 20, 2017
    (OP)

    Hi thank you so much for this it helps me a lot...sorry for my late reply because i am just busy...I really appreciated it.Thank you so much.