Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array values from textboxes

Tags:

c#

I have a 16 element int array and 16 textboxes (textBox1, textBox2 ....) that look like 4x4 matrix. Is there any way to put textboxes values to every array element not using code like this:

array[1] = (int)textBox1.Text;
array[2] = (int)textBox2.Text;
like image 724
Bat Man Avatar asked Dec 10 '25 09:12

Bat Man


2 Answers

One possibility would be to store the references to the TextBox instances in an array.

TextBox[] Boxes;

And then use a 'for' loop to populate the values.

for (int i = 0; i < 16; i++)
{
   array[i] = (int)Boxes[i].Text;
}
like image 123
Codor Avatar answered Dec 11 '25 23:12

Codor


You could use a function to get the text box's text as an integer using it's "index" from the form's control collection:

int GetBoxText(int index)
{
  return Convert.ToInt32(this.Controls["textBox" + i.ToString()].Text);
}

Note that this has no error checking of any kind. You could add some if you wanted to. All this does is get the text of the control named textBox + whatever i is from the form's control collection and convert it to an integer.

like image 40
BurningLights Avatar answered Dec 11 '25 22:12

BurningLights



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!