【问题标题】:Removing control from a dynamic interface [closed]从动态界面中删除控制[关闭]
【发布时间】:2012-05-02 12:35:28
【问题描述】:

我正在做一个 c# 练习项目,以提高我的技能并学习新事物。我创建了一个由几个控件组成的动态界面。当加载表单时,它以 numericUpDown 和 Button 启动。当用户在 numeric up down 上选择一个数字并单击按钮时,它将根据在 numericUpDown 上选择的数字生成尽可能多的文本框,它还会在生成的文本框旁边生成一个删除按钮。当用户单击删除按钮时,我无法删除文本框

这是我拥有的代码:

// 生成文本框和按钮

private void AssessmentButton_Click(object sender, EventArgs e)
{
  int length = (int)this.NoAssesmentBoxlv4.Value;
  for (int i = 0; i < length; i++)
  {
    textboxAssesmentName.Add(new TextBox());
    var p = new System.Drawing.Point(110, 260 + i * 25);                     
    (textboxAssesmentName[i] as TextBox).Location = p;
    (textboxAssesmentName[i] as TextBox).Size = new System.Drawing.Size(183, 20);
    this.Lv4Tab.Controls.Add(textboxAssesmentName[i] as TextBox);
    buttoRemove.Add(new Button());
    (buttoRemove[i] as Button).Location = new System.Drawing.Point(380, 260 + i * 25);
    (buttoRemove[i] as Button).Text = @"x";
    (buttoRemove[i] as Button).BackColor = Color.Red;
    (buttoRemove[i] as Button).ForeColor = Color.White;
    (buttoRemove[i] as Button).Size = new System.Drawing.Size(22, 23);
    this.Lv4Tab.Controls.Add(buttoRemove[i] as Button);

    (buttoRemove[i] as Button).Click += this.buttoRemove_click;
  }
}

这里是删除按钮点击的来源:(这个方法不编译)

private void buttoRemove_click(object sender, EventArgs e)
{
  foreach (Object obj in textboxAssesmentName)
  {
    // THIS LINE DOES NOT COMPILE!!!
    this.Controls.Remove(textboxAssesmentName.Remove);
  }
}

任何想法将不胜感激

【问题讨论】:

  • “不起作用”是什么意思? We are not mind readers.
  • 在我的代码中没有删除任何带有红色下划线的控件

标签: c# .net visual-studio-2010 dynamic


【解决方案1】:

试试类似的东西

foreach (var control in textboxAssesmentName)
{
    this.Controls.Remove(control);
}

您现有的代码没有任何意义。

另请参阅
http://msdn.microsoft.com/en-us/library/82785s1h%28v=vs.80%29.aspx

【讨论】:

    【解决方案2】:

    我建议你使用ButtonTag 属性来存储与之关联的TextBox。要将其放入您的代码中,请将此行插入您的循环中:

    (buttoRemove[i] as Control).Tag = textboxAssesmentName[i];
    

    那么您的事件处理程序将如下所示:

    private void buttoRemove_click(object sender, EventArgs e)
    {
        this.Controls.Remove((sender as Control).Tag as Control);
    }
    

    编辑:这是我编写代码的方式(不包括拼写错误)。

    private void AssessmentButton_Click(object sender, EventArgs e)
    {
        int length = (int)this.NoAssesmentBoxlv4.Value;
        for (int i = 0; i < length; i++)
        {
            TextBox t = new TextBox();
            System.Drawing.Point p = new System.Drawing.Point(110, 260 + i * 25);                     
            t.Location = p;
            t.Size = new System.Drawing.Size(183, 20);
    
            Button b = new Button();
            b.Location = new System.Drawing.Point(380, 260 + i * 25);
            b.Text = @"x";
            b.BackColor = Color.Red;
            b.ForeColor = Color.White;
            b.Size = new System.Drawing.Size(22, 23);
            b.Click += new System.EventHandler(this.buttoRemove_click);
    
            this.Lv4Tab.Controls.Add(t);
            this.Lv4Tab.Controls.Add(b);
            textboxAssesmentName.Add(t);
            buttoRemove.Add(b);
        }
    }
    
    private void buttoRemove_click(object sender, EventArgs e)
    {
        Control b = sender as Control;
        Control t = b.Tag as Control;
        this.Lv4Tab.Controls.Remove(t);
        this.Lv4Tab.Controls.Remove(b);
        textboxAssesmentName.Remove(t);
        buttoRemove.Remove(b);
    }
    

    【讨论】:

    • 这只会删除删除按钮,如果我按两次生成按钮,它会保留生成的副本,因此 id 必须按两次删除按钮才能删除它
    猜你喜欢
    • 2012-12-03
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多