0

i currently have a form which i dynamically created a 2D array of textboxes, buttons etc. i just found out my other parts of the program cannot access to the textboxes i created? is there a way i can do it?

my codes goes something like:

    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//////
    }

i cannot access the textbox outside the method, how can i do it? can you provide some working coded? i am still relatively new to c#, thank you very much

GameScripting
  • 16,092
  • 13
  • 59
  • 98
Clayton Leung
  • 269
  • 1
  • 3
  • 11

4 Answers4

0

You could add a class level property just like

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

            }
        }
    }
}

Then you can simply do like

Form1 myForm = GetMyForm();
System.Windows.Forms.TextBox[,] theTextboxArray = myForm.textbox;
GameScripting
  • 16,092
  • 13
  • 59
  • 98
  • i try to copy your code into my program, but it has error, "The name 'GetMyForm' does not exist in the current context", i know it is very simple to solve but my brain is struck right now, how can i fix this? – Clayton Leung Jul 21 '12 at 11:11
  • `GetMyForm` does not exist in the current context, cause it's not defined somewhere. You have to tell the compiler what a `GetMyForm` method is. In this cause I used it as a placeholder function, cause to access the textboxes you need to have a reference to your form. If you don't know where this is, have a look at the `Program.cs` file and the `static void Main()` method. There should be a `new Form1()` call. If that doen't help you anyway, please consider consuming some tutoials on object oriented programming in c#. – GameScripting Jul 21 '12 at 11:17
  • @ClaytonLeung: `GetMyForm` is just a placeholder for the method you use to get a reference to Form1. If you don't know how to do that, I'd suggest that you start a new question and, there, add a bit more detail about those "other parts of the program" where you want to access Form1's textbox. – Heinzi Jul 21 '12 at 11:19
  • oh ok, i know it is very trivial, i just not sharp enough to pick up myself. why don't you say "Form1 myform = new Form1();" in the first place? i am more of a practical learner, i know how to get things done and learn by examples, but when i read books, i dont have a clue what it means – Clayton Leung Jul 21 '12 at 17:15
0

Surely you can't access them with textbox_i_j where i & j are numerals with intellisense because its not supported but you can get them like this

TextBox GetTB(string name)
{
 return ( Controls.FindControl(name) as TextBox );
} 
yogi
  • 19,175
  • 13
  • 62
  • 92
0
Declare int column = 4;
System.Windows.Forms.TextBox[,] textbox = new System.Windows.Forms.TextBox[column, row];

outside of Form1 constructor. Your code should look like:

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//////
}
Sergiu Cojocaru
  • 687
  • 6
  • 16
0

i have search the net for answers while i wait for some smart people to answer my questions. i use a dictionary to retrieve the text box, goes something like this:

Get a Windows Forms control by name in C#

//declaration

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

// creation of array

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

            }
        }

// retrieval

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

        retrieve.Text = "apple";
Community
  • 1
  • 1
Clayton Leung
  • 269
  • 1
  • 3
  • 11