C#通过复选框删除动态文本框 [英] C# deleting dynamic textboxes by checkboxing

查看:126
本文介绍了C#通过复选框删除动态文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a code in C# I have 2 Forms and the code creates textboxes and corresponding checkboxes dynamically. The code I wrote creates dynamic textboxes and checkboxes successfully. However, I am not able to delete the row of textboxes in a selected checkbox line.

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> / code>。当您选中复选框以删除该行时,获取复选框的名称,在 List< Controls> 中搜索。所以这种方式可以获得复选框行的索引点击。现在删除该行的控件。

SOLUTION1: While creating all the controls, add them to a List<Controls>. When you are checking the checkbox to delete the row, get the name of the checkbox, search it in the List<Controls>. So this way can get the index of the row of the checkbox clicked. Now delete the controls of that rows.

解决方案2:在TablelayoutPanel中创建控件,一切都很容易。

SOLUTION2: Create your controls in a TablelayoutPanel and everything will be easy.

EDIT

复制粘贴form1中的所有内容,se btn_click作为按钮的事件处理程序。让表单的大小有点大。一切都应该正常工作。如果不工作,请告诉我。

Copy paste everything in the form1, se btn_click as a event handler for a button. Let the size of the form a bit big. Everything should work fine now. If not working, let me know.

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;
    }

这篇关于C#通过复选框删除动态文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆