locked
how to create event click in runtime for many buttons RRS feed

  • General discussion

  • i have a form in c# with a textbox and button

    the user will enter a number in textbox and when he click on tat butto will generate number of buttons equal to that number in textbox in runtime.

    i have a for loop to loop and reate the buttons and give name for eachone like 1,2,3,4,5,6,7,8,9

    my problem is how to handel the click event for this buttons?

    please any help in this


    Thanks for any help :) my blog is: http://www.waelk.com

    • Moved by CoolDadTx Tuesday, August 7, 2012 2:00 PM Winforms related (From:Visual C# General)
    Tuesday, August 7, 2012 12:19 AM

All replies

  • You need to add a handle like below for your buttons.

    this.btnGetDemand.Click += new System.EventHandler(this.btnGetDemand_Click);

    It can be the same event.

    chanmm


    chanmm

    Tuesday, August 7, 2012 3:02 AM
  • Chanmm thanks for your help but to use your code i need to defind the button and to have the button name but in my case the button name will generate in runtime and it can be like A1,A2,A3 depend of how many button (the number inside the textbox)

    this is my code

     private void button1_Click(object sender, EventArgs e)
            {
                int row = int.Parse(txtRows.Text.ToString());
                int columns = int.Parse(txtColumns.Text.ToString());
                int initialFirst = 25;
                int intialSecond = 100;
                char SeatAlpha = 'A';
                for (int x = 1; x <= row; x++)
                {
                    for (int i = 1; i <= columns; i++)
                    {
                        CreateButtons(SeatAlpha.ToString() + i, initialFirst, intialSecond);
                        initialFirst = initialFirst + 60;
                        
                    }
                    
                    initialFirst = 25;
                    intialSecond = intialSecond + 35;
                  
                   
                }
            }
            public void CreateButtons(string btnName, int btnPointFirst, int btnPointSecond)
            {
                Button myText = new Button();
                myText.Name = btnName;
                myText.Location = new Point(btnPointFirst, btnPointSecond);
                myText.Width = 50;
                myText.Height = 25;
                myText.Text = btnName;
                this.Controls.Add(myText);
            }


    Thanks for any help :) my blog is: http://www.waelk.com

    Tuesday, August 7, 2012 11:02 AM
  • Chanmm thanks for your help but to use your code i need to defind the button and to have the button name but in my case the button name will generate in runtime and it can be like A1,A2,A3 depend of how many button (the number inside the textbox)

    this is my code

     private void button1_Click(object sender, EventArgs e)
            {
                int row = int.Parse(txtRows.Text.ToString());
                int columns = int.Parse(txtColumns.Text.ToString());
                int initialFirst = 25;
                int intialSecond = 100;
                char SeatAlpha = 'A';
                for (int x = 1; x <= row; x++)
                {
                    for (int i = 1; i <= columns; i++)
                    {
                        CreateButtons(SeatAlpha.ToString() + i, initialFirst, intialSecond);
                        initialFirst = initialFirst + 60;
                        
                    }
                    
                    initialFirst = 25;
                    intialSecond = intialSecond + 35;
                  
                   
                }
            }
            public void CreateButtons(string btnName, int btnPointFirst, int btnPointSecond)
            {
                Button myText = new Button();
                myText.Name = btnName;
                myText.Location = new Point(btnPointFirst, btnPointSecond);
                myText.Width = 50;
                myText.Height = 25;
                myText.Text = btnName;
                this.Controls.Add(myText);
            }


    Thanks for any help :) my blog is: http://www.waelk.com

    you mean to say, after you add the button and button event with Button1.Click += new EventHandler(this.Button1);, how to write the method for that.

    Maybe you can generate a single EvenHandler method, there you read the text of the button and perform accordingly. 

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.click.aspx

    You create the buttons, associate the clicked event methods to single method as mentioned above:

    Button1.Click += new EventHandler(this.Btn_Click);

    Button2.Click += new EventHandler(this.Btn_Click);

    /////////

    void Btn_Click(Object sender, EventArgs e)

    {

    Button clickedButton = (Button)sender;

    if(clickedButton.Text == "Button1") { // do for button1}

    else if(clickedButton.Text == "Button2") { // do for button2}

    }

    // i have not tested the code, consider you need to modify some part

    regards

    joon



    • Edited by Joon84 Tuesday, August 7, 2012 11:22 AM
    Tuesday, August 7, 2012 11:20 AM
  • not exactly if you read my code you will find i just create a buttons not only one many buttons and add it to the form i dont have button1 to add his code Button1.Click += new EventHandler(this.Button1);

    Thanks for any help :) my blog is: http://www.waelk.com

    Tuesday, August 7, 2012 11:32 AM
  •         public void CreateButtons(string btnName, int btnPointFirst, int btnPointSecond)
            {
                Button myText = new Button();
                myText.Name = btnName;
                myText.Location = new Point(btnPointFirst, btnPointSecond);
                myText.Width = 50;
                myText.Click += new EventHandler(Button_Click);
                myText.Height = 25;
                myText.Text = btnName;
                this.Controls.Add(myText);
            }
            void Button_Click(Object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                // now you need some logic here to identify which button event is fired, if A1,A2,A3 etc.
            }
    As joon try to say, you just need to declare the event in your CreateButtons. Then in the event you make some logic to see which button is fired.

    Web Developer

    Tuesday, August 7, 2012 11:43 AM