c#通过复选框删除动态文本框
本文关键字:动态 文本 删除 复选框 | 更新日期: 2023-09-27 18:10:43
我正在用c#编写代码,我有2个表单,代码动态创建文本框和相应的复选框。我编写的代码成功地创建了动态文本框和复选框。但是,我无法删除选定复选框行中的文本框行。
public void CreateTextBox(int i, StringReader sr)
{
ProductForm form2 = new ProductForm();
_cb = new CheckBox[i];
form2.Visible = true;
form2.Activate();
int x = 10;
int y = 30;
int width = 100;
int height = 20;
for (int n = 0; n < i; n++)
{
String line = sr.ReadLine();
String[] line_ = line.Split(new char[] {''t'});
String cbName = "chkBox_" + n.ToString();
_cb[n] = new CheckBox();
_cb[n].Name = cbName;
_cb[n].Location = new Point(2, y);
_cb[n].Checked = false;
form2.Controls.Add(_cb[n]);
if (line.Length > 3)
{
for (int row = 0; row < 4; row++)
{
String name = "txtBox_" + row.ToString();
TextBox tb = new TextBox();
tb.Name = name;
tb.Text = line_[row].ToString();
tb.Location = new Point(x, y);
tb.Height = height;
if (row == 1)
{
tb.Width = width * row;
}
if (row == 3)
{
tb.Width = width * 5;
}
else
{
tb.Width = width - 20;
}
x += 10 + width;
form2.Controls.Add(tb);
}
}
x = 10;
y += 25;
}
}
private void DeleteTextBoxButton_Click(object sender, EventArgs e)
{
//Here should I add a code in order to delete dynamically created
//textboxes by clicking checkbox and button
}
}
不确定你的问题。但如果我是对的,这可能会奏效。
解决方案1:在创建所有控件时,将它们添加到List<Controls>
。当您选中复选框以删除该行时,获取复选框的名称,在List<Controls>
中搜索它。这样就能获得选中复选框的行索引。现在删除这些行的控件。
解决方案2:在TablelayoutPanel中创建控件,一切都会很容易。
编辑
复制粘贴form1中的所有内容,使用btn_click作为按钮的事件处理程序。让窗体的大小稍微大一点。现在一切都好了。如果不行,请告诉我。class MyControl
{
public TextBox txt1 { get; set; }
public TextBox txt2 { get; set; }
public TextBox txt3 { get; set; }
public TextBox txt4 { get; set; }
public CheckBox cb { get; set; }
public MyControl(TextBox txt1, TextBox txt2, TextBox txt3, TextBox txt4, CheckBox cb)
{
this.txt1 = txt1;
this.txt2 = txt2;
this.txt3 = txt3;
this.txt4 = txt4;
this.cb = cb;
}
}
List<MyControl> list = new List<MyControl>();
public int x = 50, n = 1;
TextBox txtTemp, txt1, txt2, txt3, txt4;
CheckBox cbTemp;
private void btn_Click(object sender, EventArgs e)
{
txtTemp = new TextBox();
txtTemp.Location = new System.Drawing.Point(10, x);
txtTemp.Name = "txt1_" + n;
txt1 = txtTemp;
txtTemp = new TextBox();
txtTemp.Location = new System.Drawing.Point(120, x);
txtTemp.Name = "txt2_" + n;
txt2 = txtTemp;
txtTemp = new TextBox();
txtTemp.Location = new System.Drawing.Point(230, x);
txtTemp.Name = "txt3_" + n;
txt3 = txtTemp;
txtTemp = new TextBox();
txtTemp.Location = new System.Drawing.Point(350, x);
txtTemp.Name = "txt4_" + n;
txt4 = txtTemp;
cbTemp = new CheckBox();
cbTemp.Name = "cb1_" + n;
cbTemp.Enter += new EventHandler(cbTemp_Enter);
cbTemp.Location = new System.Drawing.Point(490, x);
this.Controls.Add(txt1);
this.Controls.Add(txt2);
this.Controls.Add(txt3);
this.Controls.Add(txt4);
this.Controls.Add(cbTemp);
list.Add(new MyControl(txt1, txt2, txt3, txt4, cbTemp));
x += 40;
n++;
}
void cbTemp_Enter(object sender, EventArgs e)
{
if ((MessageBox.Show("Are you Sure?", "Warning", MessageBoxButtons.YesNo)) == DialogResult.No)
return;
CheckBox cbMain = (CheckBox)sender;
int index = Search(cbMain);
this.Controls.Remove(list[index].txt1);
this.Controls.Remove(list[index].txt2);
this.Controls.Remove(list[index].txt3);
this.Controls.Remove(list[index].txt4);
this.Controls.Remove(list[index].cb);
}
private int Search(CheckBox cbMain)
{
int temp = 0;
foreach (MyControl item in list)
{
if(cbMain.Name == item.cb.Name)
temp = list.IndexOf(item);
}
return temp;
}
对于WinForms,我建议将生成的TextBoxes放入CheckBox的Tag字段中。然后保存所有复选框的管理列表。一旦他们单击删除按钮,就遍历checkbox集合。如果它们的状态是选中的,将TextBox从Tag字段中拉出,从表单集合中移除,然后删除它。
注意:此代码未经测试,但原则上应该可以工作。
UPDATE:阅读您的最新评论,而不是在标签中存储单个文本框,只需创建另一个列表并将整个列表存储在标签中。然后遍历delete方法中的那些。
private List<CheckBox> _checkboxes = new List<CheckBox>();
public void CreateTextBox( int i, StringReader r )
{
// ... do your stuff here
_cb[n].Tag = tb;
// ... finish up
_checkboxes.Add( _cb[n] );
}
public void DeleteTextBoxButton_Click( object sender, EventArgs e )
{
foreach( var cb in _checkboxes )
{
if( cb.Checked )
{
TextBox tb = cb.Tag as TextBox;
if( tb != null )
{
form2.Controls.Remove( tb );
}
}
}
}