如何添加带有独立点击事件的按钮
How to add buttons with independent click event
我想在运行时动态添加按钮和文本框
按钮反应不同。
ie newbutton1
链接到 texbox1 ,
newbutton2linked with
textbox2`
现在任何按钮都只会从第一个文本框打印到最后一个文本框
一个接一个。
还要考虑到我在指南的表单上已经有一个 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);
}
您可以有一个 class,例如继承自按钮 class 的 mybutton,在这个新的 class 中,您可以有一个带有文本框类型的 属性。就像下面的代码一样。在您的代码中,当您想要实例化按钮时,您可以使用 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 的回答。所以改变了什么:
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
证明截图: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;
我想在运行时动态添加按钮和文本框 按钮反应不同。
ie newbutton1
链接到 texbox1 ,
newbutton2linked with
textbox2`
现在任何按钮都只会从第一个文本框打印到最后一个文本框 一个接一个。
还要考虑到我在指南的表单上已经有一个 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);
}
您可以有一个 class,例如继承自按钮 class 的 mybutton,在这个新的 class 中,您可以有一个带有文本框类型的 属性。就像下面的代码一样。在您的代码中,当您想要实例化按钮时,您可以使用 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 的回答。所以改变了什么:
I've deleted the pre-made
button1
&textbox1
and add them programatically on theForm_load
so that I can add them in theLists
证明截图: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;