2011-10-05 55 views
1

我在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

不確定你的問題。但是,如果我是對的,這可以做到這一點。解決方案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對象的例子嗎? –

1

對於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

我真的迷失在你的解決方案中。我嘗試過,但無法讓它爲我工作。仍在嘗試。 –