C# 如何从程序的其他部分访问表单中动态生成的文本框?

C# 如何从程序的其他部分访问表单中动态生成的文本框?,c#,winforms,scope,C#,Winforms,Scope,我目前有一个表单,我动态创建了一个文本框、按钮等的2D数组。我刚刚发现程序的其他部分无法访问我创建的文本框?有什么办法我能做到吗 我的代码是这样的: public Form1() { int column = 4; System.Windows.Forms.TextBox[,] textbox = new System.Windows.Forms.TextBox[column, row]; for (int i = 0; i &

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

我的代码是这样的:

    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//////
    }
public Form1()
{
int列=4;
System.Windows.Forms.TextBox[,]TextBox=新系统.Windows.Forms.TextBox[列,行];
对于(int i=0;i

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

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

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]);

            }
        }
    }
}

当然,您不能使用
textbox\u i\u j
访问它们,其中i&j是带有intellisense的数字,因为它不受支持,但您可以这样获得它们

TextBox GetTB(string name)
{
 return ( Controls.FindControl(name) as TextBox );
} 
在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//////
}
int列=4;
System.Windows.Forms.TextBox[,]TextBox;
公共表格1()
{
textbox=new System.Windows.Forms.textbox[列,行];
对于(int i=0;i
我在网上搜索答案,等待一些聪明人回答我的问题。我使用字典检索文本框,如下所示:

//声明

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

范围可能在方法之外声明你的文本框?是的,我只是有一个大脑屁,当时我没有想到。我试图将你的代码复制到我的程序中,但它有错误,“GetMyForm的名称在当前上下文中不存在”,我知道这很容易解决,但我的大脑现在被击中了,如何修复此问题?
GetMyForm
在当前上下文中不存在,因为它未在某个位置定义。您必须告诉编译器什么是
GetMyForm
方法。在本例中,我将其用作占位符函数,以便访问需要引用表单的文本框。如果您不知道它在哪里,请查看
Program.cs
文件和
static void Main()
方法。应该有一个
newform1()
调用。如果这对你没有帮助,请考虑在C语言中使用面向对象编程的一些教程。@ ClaytonLeung:<代码> GETMyFrase>代码>只是用于获取对Frim1的引用的方法的占位符。如果你不知道怎么做,我建议你开始一个新的问题,在那里,添加更多关于那些“程序的其他部分”的细节,在那里你想访问Form1的文本框。哦,好吧,我知道这是非常琐碎的,我只是没有足够的敏锐来振作起来。你为什么不首先说“Form1 myform=new 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//////
}
  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";