[英]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` 即
newbutton1
与newbutton1
链接texbox1 ,
newbutton2 linked with
textbox2 linked with
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 还请考虑我已经在指南的表单上有一个button1和textbox1
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 . 您可以拥有一个类似mybutton的类,该类继承自button类,在这个新类中,您可以拥有一个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. 在代码中,当您要实例化Button时,可以使用
list<mybutton>
并为文本框设置linkedTextbox属性。
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 : 谢谢大家, 我按照@Franck的回答进行了 更改 。
I've deleted the pre-made
button1
&textbox1
and add them programatically on theForm_load
so that I can add them in theLists
我已经删除了预制的
button1
和textbox1
并以编程方式将它们添加到Form_load
以便可以将它们添加到Lists
A proof screenshot : http://prntscr.com/aprqxz 证明屏幕截图: 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;
编辑1:由于计数从0开始,所以我没有添加
newButton.Tag = buttons.count+1;
I added just newButton.Tag = buttons.count;
我只添加了
newButton.Tag = buttons.count;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.