使用tag属性从面板中删除动态添加的控件 [英] removing the dynamical added control from the panel using tag property
本文介绍了使用tag属性从面板中删除动态添加的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有按钮的窗体,当点击按钮时它会动态生成控件,还会添加一个动态生成的按钮,这样它就会删除符合条件的控件,意味着一边按钮,一排单击按钮时我的代码将被删除
i have windows form which have a button, when button is clicked it dynamically generated controls, also a button which is dynamically generated is added so that it will remove the controls which are in line, means aside the button, a row of control will be removed when button clicked my code is
int c = 0;
private void button1_Click(object sender, EventArgs e)
{
int v;
v = c++;
panel1.VerticalScroll.Value = VerticalScroll.Minimum;
Button btn = new Button();
btn.Name = "btn" + v;
btn.Text = "Remove";
btn.Location = new Point(750, 5 + (30 * v));
btn.Click += new EventHandler(btn_Click);
ComboBox combo = new ComboBox();
combo.Name = "combobox" + v;
combo.Location = new Point(30, 5 + (30 * v));
combo.Tag = btn;
ComboBox combo2 = new ComboBox();
combo2.Name = "combobox2" + v;
combo2.Location = new Point(170, 5 + (30 * v));
combo2.Tag = btn;
TextBox txt = new TextBox();
txt.Name = "txtbx" + v;
txt.Location = new Point(300, 5 + (30 * v));
txt.Tag = btn;
TextBox txt2 = new TextBox();
txt2.Name = "txtbx2" + v;
txt2.Location = new Point(450, 5 + (30 * v));
txt2.Tag = btn;
TextBox txt3 = new TextBox();
txt3.Name = "txtbx3" + v;
txt3.Location = new Point(600, 5 + (30 * v));
txt3.Tag = btn;
panel1.Controls.Add(combo);
panel1.Controls.Add(btn);
panel1.Controls.Add(txt);
panel1.Controls.Add(combo2);
panel1.Controls.Add(txt2);
panel1.Controls.Add(txt3);
}
private void btn_Click(object sender, EventArgs e)// this is the dynamically added button''s event which will remove the combobox and textbox
{
Button btnh = sender as Button;
foreach (Control item in panel1.Controls.OfType<TextBox>())
{
if (item.Tag == sender || item == sender)
panel1.Controls.Remove(item);
}
foreach (Control item in panel1.Controls.OfType<ComboBox>())
{
if (item.Tag == sender || item == sender)
panel1.Controls.Remove(item);
}
panel1.Controls.Remove(btnh);
}
我的错误是什么,但问题是dosnt删除了它控制的所有控件,我不知道我的代码简单易行的问题但我不知道它缺少哪里
my error is nothing but the problem is it dosnt removes all the controls it leaves controls and i dont know whats the problem my code is simple and easy but i dont know where it lacks
推荐答案
这是你能做的另一件好事。
而不是Panel使用,TableLayoutPanel,因为你说排列控件就像逐行一样。
说
Line1 - 你有一套TextBox ,ComboBox和按钮
Line2 - 包含另一组TextBox,ComboBox和Buttons
放置一个包含1行和1列的TableLayoutPanel
将Panel1控件放在Row(0),Column(0)位置并设置其Dock状态 - FILL
在Row放置另一个Panel2控件(1)列(0)位置并设置其停靠状态 - 填写
使用Panel1.Clear删除您所用语言的第1行中的所有控件。
保持Panel2中的控件就是这样。未触动!!!
Here is the another best thing you can do.
instead of Panel use, TableLayoutPanel since you said the arrangement controls is like line by line.
say
Line1 - you have set of TextBox,ComboBox and Buttons
Line2 - contains another set of TextBox,ComboBox and Buttons
Place a TableLayoutPanel with 1 row and 1 column
Place a Panel1 Control at Row(0),Column(0) position and set its Dock status - FILL
Place another Panel2 Control at Row(1) Column(0) position and set its dock status - FILL
use Panel1.Clear to remove all the controls in Line 1 in your language.
keep the controls in Panel2 as such.Untouched!!!
显然,这并不是最好的方法,即使你没有犯错。您正在寻找要删除的控件,对吧?创建后在标签中引用它。什么标签?在点击时已经知道的东西:按钮。例如:
It is not a best way, apparently, even if you did not make a mistake in it. You are looking for a control to be removed, right? Make a reference to it a tag after creation. A tag of what? Of something which is already known at the moment of the click: the button. For example:
ComboBox aComboBoxToBeRemovedLater = new ComboBox();
//...
Button aRemovingButton = new Button();
aRemovingButton.Text = "&Remove";
aRemovingButton.Tag = aComboBoxToBeRemovedLater;
aRemovingButton.Click += (sender, eventArgs) => {
Button thisButton = (Button)sender;
Control controlToRemove = (Control)thisButton.Tag;
thisButton.Parent.Controls.Remove(controlToRemove);
//... maybe even remove the button itself, adjust layout, etc...
} //aRemovingButton.Click
//...
// add the button, the combo box and other controls to the parent here
这篇关于使用tag属性从面板中删除动态添加的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文