0

例如:

在运行时以 1 个文本框(名称:textbox1)和 1 个标签(名称:label1)开头的表单创建文本框和标签(侧面),因此在运行时我们可以拥有

label1 - textbox1
label2 - textbox2
label3 - textbox3
label4 - textbox4

如何在编译可执行文件之前在代码中引用这些期货文本框/标签,而不会出现这些文本框/标签不存在(尚)的错误?

只有每个人都知道,我以这种方式在运行时创建新的文本框和标签:

            n++;
            TextBox txt = new TextBox();
            txt.Name = "textbox" + n;
            txt.Text = "";
            txt.Size = new System.Drawing.Size(189, 26);
            txt.Location = new Point(87, n2);
            testelogico = txt.Name;
            gpbCategoria.Controls.Add(txt);
            txt.TextChanged += new EventHandler(new_onchange);
            txt.Leave += new EventHandler(erase_onLeave);

            Label lbl = new Label();
            lbl.Name = "label" + n;
            lbl.Text = "Acessório Nº" + n + ":";
            lbl.Location = new Point(4, n2 + 5);
            gpbCategoria.Controls.Add(lbl);

在另一部分代码中,我想参考例如:

If (textbox4.Text == "" && label4.Name == "Acessório Nº4:")
{
gpbCategoria.Controls.Remove(textbox4);
gpbCategoria.Controls.Remove(label4);
}

但我会有错误,因为这些标签还不存在(只会在运行时创建)

4

5 回答 5

2

textBox4您正在动态生成控件,因此编译器甚至在创建之前都不知道是什么。您可以做的是在运行时按名称搜索该控件:

TextBox textbox4 = (TextBox)this.Controls.Find("textbox4", false).FirstOrDefault();

if (textbox4 == null)
{
    throw new Exception("Could not find textbox4.");
}

这将搜索textbox4inForm.Controls并在不存在时抛出异常。labels您可以对表单中的任何其他控件遵循相同的模式。

于 2013-05-02T20:05:07.467 回答
0

嗨@programmer93 和@Jonesy,感谢您的帮助,现在它工作正常,请查看我的最终代码(可以帮助与我有同样疑问的人)

    TextBox txtAcessorio4 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio4", false).FirstOrDefault();
Label lblAcessorio4 = (Label)gpbCategoria.Controls.Find("lblAcessorio4", false).FirstOrDefault();

                    if (txtAcessorio4 != null && txtAcessorio4.Text == "" && lblAcessorio4.Name == "lblAcessorio4")
                    {
                        MessageBox.Show("Perfect");                           
                    }
于 2013-05-02T20:44:09.583 回答
0

将Label - TextBox pare 放入UserControl 会更好。

让 UserControl 具有通过构造函数传递的索引号

   public class MyUserControl : UserControl 
   {
      private readonly int index;

      public MyUserControl(int index)
      {
         this.index = index;

         InitializeComponent(); // will init you sub controls: label and textbox

         // set name to label

      } 

      public int Index
      {
         get { return index; }
      } 
   }

使用已经建议的方法通过 Controls 集合中的索引查找您的用户控件,如果找到则将其删除。

于 2013-05-02T20:23:08.230 回答
0

如果您使用的是 .Net 框架 4.0 或更高版本,则可以使用 dynamic 关键字:以下是完整代码供您参考:

public class Class1
{
    // Declare all the controls as dynamic
    dynamic textbox1, textbox2, textbox3, textbox4;
    dynamic label1, label2, label3, label4;

    public Class1()
    {
        // Create the actual object type, which they will hold at Run time. 
        textbox1 = textbox2 = textbox3 = textbox4 = new TextBox();
        label1 = label2 = label3 = label4 = new Label();

        // Loop through to create controls at Runtime.
        n++;
        TextBox txt = new TextBox();
        txt.Name = "textbox" + n;
        txt.Text = "";
        txt.Size = new System.Drawing.Size(189, 26);
        txt.Location = new Point(87, n2);
        testelogico = txt.Name;
        gpbCategoria.Controls.Add(txt);
        txt.TextChanged += new EventHandler(new_onchange);
        txt.Leave += new EventHandler(erase_onLeave);

        Label lbl = new Label();
        lbl.Name = "label" + n;
        lbl.Text = "Acessório Nº" + n + ":";
        lbl.Location = new Point(4, n2 + 5);
        gpbCategoria.Controls.Add(lbl);

    }

    public void Foo()
    {
        //Throw exception if controls are not initialized yet.
        if (textbox4 == null || label4 == null)
        {
            throw new Exception("Controls not initialized.");
        }

        else
        {
            // You can access the control propoties similar to normal controls.
            if (textbox4.Text == "" && label4.Name == "Acessório Nº4:")
            {
                gpbCategoria.Controls.Remove(textbox4);
                gpbCategoria.Controls.Remove(label4);
            }
        }
    }
}
于 2013-05-02T20:11:30.950 回答
0

您可以按名称找到文本框:

var textbox = this.Controls.OfType<TextBox>().Single(ctr => ctr.Name == "textboxname");
于 2013-05-02T20:06:53.390 回答