ボタンとテキストボックスを実行時に動的に追加したい場合は、それぞれの ボタンの反応が異なります。独立したクリックイベントでボタンを追加する方法
texbox1 ,
newbutton2 linked with
textbox2`今
とリンクnewbutton1
すなわち、いずれかのボタンは、単に最初から最後のテキストボックス 次々に出力します。
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);
}
を追加しましたか? –
リストにaddignの前に 'newButton.Tag = buttons.count + 1'をセットし、次にclickイベントでこのような保存されたインデックスを使ってテキストボックスを取得します。var txt = textboxes [Convert.ToIn32((Button)sender) .Tag)]; ' – Franck