繁体   English   中英

如何添加具有独立单击事件的按钮

[英]How to add buttons with independent click event

我想在运行时动态添加按钮和文本框,每个按钮的反应都不同。

newbutton1newbutton1链接texbox1 , newbutton2 linked with textbox2 linked with

现在,任何按钮都只会从第一个到最后一个文本框一个接一个地打印。

还请考虑我已经在指南的表单上有一个button1和textbox1

这是我的代码:

        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);

        }

您可以拥有一个类似mybutton的类,该类继承自button类,在这个新类中,您可以拥有一个textbox type属性。 就像下面的代码。 在代码中,当您要实例化Button时,可以使用list<mybutton>并为文本框设置linkedTextbox属性。

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

在您的代码中,您应该编写如下内容:

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

并且在您可以使用的情况下,您可以使用:

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

谢谢大家, 我按照@Franck的回答进行了 更改

我已经删除了预制的button1textbox1并以编程方式将它们添加到Form_load以便可以将它们添加到Lists

证明屏幕截图: http : //prntscr.com/aprqxz

码:

        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);
        }

编辑1:由于计数从0开始,所以我没有添加newButton.Tag = buttons.count+1; 我只添加了newButton.Tag = buttons.count;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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