SubZero SubZero - 1 month ago 12
C# Question

how do you store a dynamical set of textboxs to an array in a c# winform

I'm currently trying to program what I thought was going to be a simple game, since I have already done it in a non-graphical environment (c++). Now im trying to reprogram in c# winform. In my first form the user inputs how many players are going to play in a range from 1 to 10. Depending on the users input on the first form determines how many labels and textboxs will be populated on my second form.

On my second form where the textboxs are populated on load, I am trying to take the name(s) the user or users put into the textbox or textboxs and store them into an array I can pass to my next form and use to display the users name when it is there turn to play.

private void playerNames_Load(object sender, EventArgs e)
{
string myOnload = null;

if (myOnload == null)
{
TextBox[] textBox = new TextBox[numPlayers];
Label[] label = new Label[numPlayers];
int labelX, labelY, textboxX, textboxY;

labelX = 20;
labelY = 22;
textboxX = 70;
textboxY = 20;

for (int i = 0; i < numPlayers; i++)
{
int labelCTR = i + 1;
textBox[i] = new TextBox();
textBox[i].Name = "player" + i;
textBox[i].Text = "";
textBox[i].Location = new Point(textboxX, textboxY);

label[i] = new Label();
label[i].Name = "player" + i;
label[i].Text = "Player " + labelCTR;
label[i].Location = new Point(labelX, labelY);

labelY += 25;
textboxY += 25;
}

for (int i = 0; i < numPlayers; i++)
{
this.Controls.Add(textBox[i]);
this.Controls.Add(label[i]);
}
}
}


The above code is how I make the form populate the textBoxs based on the user input for the value of the numPlayers variable.

private void submitPlayers_Click(object sender, EventArgs e)
{
string[] name = new string[numPlayers];
//List<string> name = new List<string>();

for (int i = 0; i < numPlayers; i++)
{
TextBox[] textBox = new TextBox[numPlayers];
//name[i] = textBox[i].Text; // first try and fail
name[i] += textBox[i].Text; // my code currently crashes here
//name.Add(textBox[i].Text); // it also crashes here if i try to switch from array to list


for (i = 0; i < numPlayers; i++)
{
int m = i+1;
MessageBox.Show("Player " + m + " " + name[i], "testing");
}
}


The above code is where I'm trying to dump the textBoxs into an array so I can store them for later use on my next form. I added the messageBox at the end to try and test the array by making it dump one name per messageBox until all the names have been successfully dumped. When I finally get that to work, the for loop with the messageBox will be replaced with the code to open my next form and pass the name array to it. So each players turn will be a little more personalized with their name in the title bar of the form.

Jcl Jcl
Answer

You are making the textboxes local to your loops, so you can't access the original textboxes from the second method (you are accessing a new array which you haven't populated).

This:

TextBox[] textBox = new TextBox[numPlayers];

Creates a new array.

So declare them scoped to the class (where you are declaring numPlayers, for example), instead of on the method, and when creating the array, use:

textBox = new TextBox[numPlayers];

(notice that we aren't specifying the type, so here we are assigning an array to an existing variable, instead of declaring a new variable)

In the second method, just remove the declaring line alltogether (the array is already declared, and has been created on the first method)


The reason it crashes is because you are doing:

TextBox[] textBox = new TextBox[numPlayers];
name[i] += textBox[i].Text;

So you are allocating an array of textboxes of size numPlayers, but you are not creating the textboxes themselves (notice that, in the first method, you are doing so, by doing textBox[i] = new TextBox())... so when you try to access textBox[i] on the second method, you are accesing an unintialized variable.