0

我目前有一个表单,我动态创建了一个文本框、按钮等的二维数组。我刚刚发现程序的其他部分无法访问我创建的文本框?有什么办法可以做到吗?

我的代码类似于:

    public Form1()
    {
        int column = 4;

        System.Windows.Forms.TextBox[,] textbox = new System.Windows.Forms.TextBox[column, row];

        for (int i = 0; i < column; i++)
        {
            for (int j = 0; j < row; j++)
            {
                textbox[i, j] = new System.Windows.Forms.TextBox();
                textbox[i, j].Size = new Size(80, 20);
                textbox[i, j].Name = "textbox_" + i + "_" + j;
                textbox[i, j].Location = new System.Drawing.Point((i * 80) + 20, (j * 20) + 30);
                textbox[i, j].Visible = true;
                Controls.Add(textbox[i, j]);

            }
        }

        /////fill the textboxes with data//////
    }

我无法访问方法之外的文本框,我该怎么做?你能提供一些工作编码吗?我对c#还是比较陌生,非常感谢

4

4 回答 4

0

您可以添加一个类级别的属性,就像

public class Form1
{
    public System.Windows.Forms.TextBox[,] textbox { get; set; }

    public Form1()
    {
        int column = 4;

        textbox = new System.Windows.Forms.TextBox[column, row];

        for (int i = 0; i < column; i++)
        {
            for (int j = 0; j < row; j++)
            {
                textbox[i, j] = new System.Windows.Forms.TextBox();
                textbox[i, j].Size = new Size(80, 20);
                textbox[i, j].Name = "textbox_" + i + "_" + j;
                textbox[i, j].Location = new System.Drawing.Point((i * 80) + 20, (j * 20) + 30);
                textbox[i, j].Visible = true;
                Controls.Add(textbox[i, j]);

            }
        }
    }
}

然后你可以简单地做

Form1 myForm = GetMyForm();
System.Windows.Forms.TextBox[,] theTextboxArray = myForm.textbox;
于 2012-07-21T10:33:42.423 回答
0

当然,您无法使用textbox_i_j其中 i 和 j 是带有智能感知的数字来访问它们,因为它不受支持,但您可以像这样获得它们

TextBox GetTB(string name)
{
 return ( Controls.FindControl(name) as TextBox );
} 
于 2012-07-21T10:35:49.193 回答
0
Declare int column = 4;
System.Windows.Forms.TextBox[,] textbox = new System.Windows.Forms.TextBox[column, row];

在 Form1 构造函数之外。您的代码应如下所示:

int column = 4;
System.Windows.Forms.TextBox[,] textbox;
public Form1()
{


    textbox = new System.Windows.Forms.TextBox[column, row];

    for (int i = 0; i < column; i++)
    {
        for (int j = 0; j < row; j++)
        {
            textbox[i, j] = new System.Windows.Forms.TextBox();
            textbox[i, j].Size = new Size(80, 20);
            textbox[i, j].Name = "textbox_" + i + "_" + j;
            textbox[i, j].Location = new System.Drawing.Point((i * 80) + 20, (j * 20) + 30);
            textbox[i, j].Visible = true;
            Controls.Add(textbox[i, j]);

        }
    }

    /////fill the textboxes with data//////
}
于 2012-07-21T10:36:35.990 回答
0

我在网上搜索答案,等待一些聪明人回答我的问题。我使用字典来检索文本框,如下所示:

在 C# 中按名称获取 Windows 窗体控件

//宣言

  Dictionary <String, System.Windows.Forms.TextBox> dictionaryTextBox = new Dictionary<string, System.Windows.Forms.TextBox>();

// 创建数组

 System.Windows.Forms.TextBox[,] textbox = new System.Windows.Forms.TextBox[column, row];

        for (int i = 0; i < column; i++)
        {
            for (int j = 0; j < row; j++)
            {
                textbox[i, j] = new System.Windows.Forms.TextBox();
                textbox[i, j].Size = new Size(80, 20);
                textbox[i, j].Name = "textbox_" + i + "_" + j;
                textbox[i, j].Location = new System.Drawing.Point((i * 80) + 20, (j * 20) + 30);
                textbox[i, j].Visible = true;
                Controls.Add(textbox[i, j]);


                  //new added
                dictionaryTextBox.Add (textbox[i, j].Name, textbox[i, j]);

            }
        }

// 恢复

        System.Windows.Forms.TextBox retrieve = dictionaryTextBox["textbox_3_3"];

        retrieve.Text = "apple";
于 2012-07-21T11:15:15.727 回答