【问题标题】:Remove dynamic TextBox'es on CheckedListBox SelectedIndexChanged [closed]删除 CheckedListBox SelectedIndexChanged 上的动态文本框 [关闭]
【发布时间】:2015-11-17 07:16:22
【问题描述】:

当我检查 CheckBoxList Items 时,会添加动态 texbox。但是在未选中时,我想删除特定的文本框。该代码在添加文本框时运行良好,但在删除时给了我例外。任何帮助都会很棒。

我的代码:

 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())

// 如果条件是最上面的文本框而不是点击的文本框被删除,我跳过这里。

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

                break;
            }
    } 

我也试过了

  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;

                }
            }
        }
    }

【问题讨论】:

  • 请提供具体的异常信息。
  • 无效的 CastException 详细信息无法将“System.Windows.Forms.CheckedListBox”类型的对象转换为“System.Windows.Forms.TextBox”类型。
  • 这个错误发生在哪里?
  • @manraj 不要对我们发号施令。我们在这里试图免费为您提供帮助。实际上,我们需要时间来提供帮助,因此您至少可以对社区友好。
  • 我已经更新了这个问题。它也不适用于checkedListBox1_ItemCheck。

标签: c# .net winforms checkboxlist


【解决方案1】:

this.Controls 会有多个控件:

 else if (checkedListBox1.GetItemCheckState(iy) == CheckState.Unchecked)
        {
            foreach (Control con in this.Controls)
            {
                if(con is TextBox)
                {
                    if (t.Name == iy.ToString())
                    {
                      this.Controls.Remove(t);
                      t.Dispose();
                      break;
                    }
                }
            }

            break;
        }

另外,如果它不起作用,请调试您的代码并查看控件是如何流动的

【讨论】:

  • 你能调试你的代码并检查控制是否流向:this.Controls.Remove(t);与否。
  • 我怀疑checkedListBox1的threestate是否设置为true,并且它的状态可能是Intermediate而不是未选中的..
  • 是的,亲爱的。当断点进入内部循环时,t.Name 为空。如果控件通过名称成功添加,请对我进行排序
  • 我已经更新了物品检查的代码。你能更新这个吗
【解决方案2】:

假设我猜到了错误的正确来源:

 foreach (TextBox t in this.Controls)
 {
       if (t.Name == iy.ToString())
       {
            this.Controls.Remove(t);
            t.Dispose();
            break;
       }
 }

可能可能工作,但也可能可能失败。不能仅仅假设this.Controls 中的所有Controls 都属于TextBox 类型。请注意,this.Controls 将返回特定上下文中的所有 Controls (this)。

解决方案是检查它是否为TextBox

    foreach (var control in this.Controls)
    {
        if (control is TextBox)
        {
            if (t.Name != null && t.Name == iy.ToString())
            {
                this.Controls.Remove(t);
                t.Dispose();
                break;
            }
        }
    }

【讨论】:

  • 不是很有建设性.. 它在哪里失败了?
  • 它说 t.Name 为空
  • 我已经更新了项目检查的代码。你能更新这个吗
  • 您现在使用的方式是一种非常糟糕的做法。你试过我提供的代码了吗?这应该有效。
  • 是的,亲爱的。我用过但没有得到任何解决方案
【解决方案3】:

CheckOnClick 设置为True of 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;
    }

【讨论】:

  • 亲爱的 C# 代码。不是asp.net
  • 这也是 c# :P .. 好的,我知道了 .. 你正在处理 WinForms .. 几分钟后看看我的更新 ..
  • 请检查更改..
  • 亲爱的它不工作。我已经更新了项目检查的代码。你能更新这个吗
  • 一切正常,无一例外!!! ...你能指定..not working是什么...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-01
相关资源
最近更新 更多