I have an add user button which adds a textbox and a button. I want it so that the new button removes the user it added. My problem is I don't know how to get a dynamically added button to delete dynamically created textboxes... I think its a problem with how I defined the variables but I don't know what . Here's what I have:
private void AddUserbtn_Click_1(object sender, EventArgs e)
{
TextBox[] Alias = new TextBox[n];
Button[] Remove = new Button[n];
int AliasX, AliasY, RemoveX, RemoveY;
AliasX = 40;
AliasY = 45;
RemoveX = 946;
RemoveY = 45;
for (int i = 0; i < n; i++)
{
Alias[i] = new TextBox();
Alias[i].Size = new Size(233, 26);
Alias[i].Location = new Point(AliasX, AliasY + space);
Alias[i].Font = new Font("Arial", 10);
Remove[i] = new Button();
Remove[i].Location = new Point(RemoveX, RemoveY + space);
Remove[i].Text = "";
Remove[i].Font = new Font("Arial", 10);
Remove[i].FlatStyle = FlatStyle.Flat;
Remove[i].BackgroundImage =Properties.Resources.btn_remove_user;
Remove[i].FlatAppearance.BorderColor = Color.White;
Remove[i].BackgroundImageLayout = ImageLayout.Center;
Remove[i].Size = new Size(95, 23);
Remove[i].UseVisualStyleBackColor = true;
Remove[i].Click += new EventHandler(Remove_Click);
space += 35;
}
for (int i = 0; i < n; i++)
{
Panel.Controls.Add(Alias[i]);
}
//for(int i=0; i <n;i++)
//Remove[i].Click += delegate
//{
// Panel.Controls.Remove(Alias[i]);
//};
}
private void Remove_Click(object sender, EventArgs e)
{
// Button Remove = sender as Button;
// //TextBox[] Alias = new TextBox[n];
// //for (int i = 0; i <n; i++)
// //{
// // Panel.Controls.Remove(Alias[i]);
// //}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…