删除CheckedListBox SelectedIndexChanged上的动态TextBox [英] Remove dynamic TextBox'es on CheckedListBox SelectedIndexChanged

查看:103
本文介绍了删除CheckedListBox SelectedIndexChanged上的动态TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我选中CheckBoxList项目时,将添加动态texbox。但是在未选中状态下,我想删除特定的文本框。该代码可以很好地添加文本框,但在删除文本框时会给我带来例外。任何帮助都将非常有用。

When I checked the CheckBoxList Items the dynamic texboxes are added. But on Unchecked I want to remove particular textboxes. The code is working fine for adding textboxes but gives me exception at removing.Any Help will be great.

我的代码:

 iy = 0;
 private void checkedListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (load == false)
        {
            return;
        }
            PackingDetails pd = new PackingDetails();
            var txt = new TextBox();
            for (iy = 0; iy < checkedListBox1.Items.Count; iy++)
            if (checkedListBox1.GetItemChecked(iy))
            {



                    txt.Name = iy.ToString();
                    txt.Text = iy.ToString();
                    txt.Location = new Point(23, 32 + (iy * 28));
                    txt.Visible = true;
                    this.Controls.Add(txt);
                    break;

            }

            else if (checkedListBox1.GetItemCheckState(iy) == CheckState.Unchecked)
            {
                foreach (TextBox t in this.Controls)
                {
                    if (t.Name == iy.ToString())

//如果在此条件下删除了最上面的文本框而不是单击的文本框,则在此处跳过此操作。

// here if i skip this if condition the topmost textbox but not the clicked one is deleted.

                    {
                        this.Controls.Remove(t);
                        t.Dispose();
                        break;
                    }
                }

                break;
            }
    } 

OR

我也尝试过

  int iy = 0;
    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
       TextBox txt = new TextBox();
        panel1.VerticalScroll.Value = VerticalScroll.Minimum;
        if (e.NewValue == CheckState.Checked)
        {
            txt.Name = iy.ToString();
            txt.Text = iy.ToString();
            txt.Location = new Point(23, 32 + (iy * 28));
            txt.Visible = true;
            this.Controls.Add(txt);
            count = iy;
            iy++;

        }
        else
        {
            foreach (Control control in this.Controls)
            {
                if (control is TextBox)
                {
                   // here it deletes the topmost textbox but not the clicked one. Here if I used if condition like control.Name == iy.ToString() Nothing happens
                        this.Controls.Remove(control);
                        control.Dispose();
                        break;

                }
            }
        }
    }


推荐答案

CheckOnClick 设置为 True CheckBoxList ..

    private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

        for (iy = 0; iy < checkedListBox1.Items.Count; iy++)
        {
            var item = checkedListBox1.Items[iy];
            string ctlName = item.ToString();
            Control txt = null;
            if (checkedListBox1.GetItemChecked(iy))
            {
                txt = FindControl(ctlName);
                if (txt != null)
                {
                    continue;
                }

                txt = new TextBox();
                txt.Name = ctlName;
                txt.Text = ctlName;
                txt.Location = new Point(150, 32 + (iy * 28));
                txt.Visible = true;
                this.Controls.Add(txt);                    
            }
            else if (checkedListBox1.GetItemCheckState(iy) == CheckState.Unchecked)
            {
                txt = FindControl(ctlName);
                if (txt==null)
                {
                    continue;
                }
                this.Controls.Remove(txt);
                txt.Dispose();                    
            }
        }


    }

    Control FindControl(String ctlName)
    {
        for (int i = 0; i < this.Controls.Count; i++)
        {
            if (this.Controls[i].Name==ctlName)
            {
                return this.Controls[i];
            }
        }

        return null;
    }

这篇关于删除CheckedListBox SelectedIndexChanged上的动态TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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