删除动态创建的文本框 [英] Removing dynamically created textboxes
本文介绍了删除动态创建的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个添加用户按钮,它添加了一个文本框和一个按钮.我想要它以便新按钮删除它添加的用户.我的问题是我不知道如何获得一个动态添加的按钮来删除动态创建的文本框......我认为这是我如何定义变量的问题,但我不知道是什么.这是我所拥有的:
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]);
// //}
}
推荐答案
给你的对象起有意义的名字,比如:
Give your objects meaningful names like:
Alias[i].Name = "UserTextBox" + i;
Remove[i].Name = "UserButton" + i;
这样可以找到要排除的对象.
This way you can find the object to be excluded.
Panel.Controls.Remove(Panel.Controls["UserTextBox" + i]);
Panel.Controls.Remove(Panel.Controls["UserButton" + i]);
这篇关于删除动态创建的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文