Warning: file_put_contents(/datas/wwwroot/jiajiahui/core/caches/caches_template/2/default/show.php): failed to open stream: Permission denied in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 55

Warning: chmod(): Operation not permitted in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 56
C#通过复选框删除动态文本框 - 源码之家

C#通过复选框删除动态文本框

问题描述:

我在C#中编写代码我有2个窗体,代码动态地创建文本框和相应的复选框。我写的代码成功创建了动态文本框和复选框。但是,我无法删除所选复选框行中的一行文本框。C#通过复选框删除动态文本框

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 

    } 
} 
+0

好的,你的问题是什么? – Marlon

+0

看起来像winforms,但你可以请标签这是关于winforms/webforms/WPF吗? – Oded

+1

很明显,他希望有人填写他的功能上的空白。 – wllmsaccnt

不确定你的问题。但是,如果我是对的,这可以做到这一点。解决方案1:创建所有控件时,将它们添加到List<Controls>。当您选中该复选框以删除该行时,获取该复选框的名称,然后在List<Controls>中搜索该复选框。因此,这种方式可以获得点击复选框的行的索引。现在删除那些行的控件。解决方案2:在TablelayoutPanel中创建控件,一切都很简单。

编辑

复制粘贴一切在Form1,SE 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; 
    } 
+0

我正在尝试第一个解决方案。解决方案2听起来不错,但我没有经验。 –

+0

为列表创建一个类(MyControls)与公共控制属性(如文本框,按钮和任何你有)。在创建每一行时,将一个MyControl对象添加到List中。你完成了几乎所有的事情。 – Sandy

+0

rapsalands你能写一个我如何创建一个List和MyControl对象的例子吗? –

对于WinForms,我建议将生成的TextBox放入CheckBox的Tag字段中。然后保留所有复选框的托管列表。一旦他们点击删除按钮,迭代CheckBoxes的集合。如果他们的状态被选中,将TextBox从标签字段中取出,将其从表单集合中删除,然后将其删除。

注意:此代码未经测试,但应原则上工作。

更新:读取您的最新评论,而不是将单个文本框存储在标记中,只需创建另一个列表并将整个列表存储在标记中。然后遍历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); 
      } 
     } 
    } 
} 
+0

我将完全像那个标记或通过名称字段。但是,我不知道如何访问我的按钮功能中的复选框和文本框?我如何在foreach或for循环中迭代它。 –

+0

我的示例中的_checkboxes属性在方法之外的类级别定义,因此它将在DeleteTextBoxButton_Click方法内可见。 –

+0

我真的迷失在你的解决方案中。我尝试过,但无法让它为我工作。仍在尝试。 –