简体   繁体   中英

How to add buttons with independent click event

I want to add buttons and textboxes dynamically on runtime with each button react differently.

ie newbutton1 linked with texbox1 , newbutton2 linked with textbox2`

Right now any button just prints from the first to the last textbox one after the other.

Also consider that I have a button1 & textbox1 already on the form for guides

Here is my code :

        List<Button> buttons = new List<Button>();
        List<TextBox> textboxes = new List<TextBox>();

        int NumTextBox = 0;
        void click(object sender, EventArgs e)
        {

            MessageBox.Show(textboxes[NumTextBox].Text);
            NumTextBox++;
        }

        int x = 0;
        int y = 0;
        void AddClick(object sender, EventArgs e)
        {
                Button newButton = new Button();
                buttons.Add(newButton);
                newButton.Click += click;// 
               // newButton.Location.Y = button1.Location.Y + 20;
                newButton.Location = new Point(button1.Location.X, button1.Location.Y+25+x);
                x += 25;
                this.Controls.Add(newButton);   

                TextBox newTextBox = new TextBox();
                textboxes.Add(newTextBox);
               // newTextBox.Click += click;

                newTextBox.Location = new Point(textBox1.Location.X, textBox1.Location.Y+25+y);
                y += 25;
                this.Controls.Add(newTextBox);

        }

you can have a class like mybutton that inherits from button class, in this new class you can have a property with textbox type . just like following code . and in your code when you want to Instantiated Button you can use list<mybutton> and set linkedTextbox property with a textbox.

public class myButton:Button
{
   ...
   public TextBox linkedTextBox{set;get;}
}

and in your code you should write some thing like this :

list<myButton> buttons=new list<myButton>();
Textbox someTextBox=new TextBox();
buttons[0].linkedTextbox=someTextBox;

and in your event you can use:

((myButton)sender).linkedTextBox.text="Some thing";

Thank you everyone , I followed @Franck's answer .So WHAT CHANGED :

I've deleted the pre-made button1 & textbox1 and add them programatically on the Form_load so that I can add them in the Lists

A proof screenshot : http://prntscr.com/aprqxz

CODE:

        List<Button> buttons = new List<Button>();
        List<TextBox> textboxes = new List<TextBox>();

        Button button1 = new Button();
        TextBox textBox1 = new TextBox();

        int x = 0;
        int y = 0;

        void click(object sender, EventArgs e)
        {


            var txt =  textboxes[Convert.ToInt32(((Button)sender).Tag)].Text;
            MessageBox.Show(txt.ToString());

        }

        void AddClick(object sender, EventArgs e)
        {

                Button newButton = new Button();
                newButton.Click += click;
                newButton.Location = new Point(button1.Location.X, button1.Location.Y+25+x);
                x += 25;
                newButton.Tag = buttons.Count;

                this.Controls.Add(newButton);

                buttons.Add(newButton);             
                //
                TextBox newTextBox = new TextBox();
                newTextBox.Location = new Point(textBox1.Location.X, textBox1.Location.Y+25+y);
                y += 25;
                this.Controls.Add(newTextBox);

                textboxes.Add(newTextBox);

        }
        void MainFormLoad(object sender, EventArgs e)
        {
                button1.Click += click; 
                button1.Location = new Point(55, 48);

                button1.Tag = buttons.Count;

                this.Controls.Add(button1);

                buttons.Add(button1);               
                //
                textBox1.Location = new Point(137, 50);

                this.Controls.Add(textBox1);

                textboxes.Add(textBox1);
        }

EDIT 1: As the counting starts from 0 I didn't added newButton.Tag = buttons.count+1; I added just newButton.Tag = buttons.count;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM