Reputation: 13
How can i store values from textbox in winforms to matrix. I took the values from the textbox and converted them to int, Now i would like to store them in a two-dimensional array.
int m11 = Convert.ToInt16(textbox.Text);
int m12 = Convert.ToInt16(textbox.Text);
int m13 = Convert.ToInt16(textbox.Text);
I can store them in a List of single dimensional array but how to store them in a two dimensional array do i have to use any for loops
List<Int[]> lstArray = new List<Int[]>();
int[] arr = new int[10];
arr[0] = m11;
arr[1] = m12;
arr[2] = m13;
lstArray.Add(arr);
int[,] matrixValues = new int[10,10];
Upvotes: 1
Views: 384
Reputation: 6849
Give the textbox name like txt_0_0, txt_0_1,..txt_0_9, txt_1_0, txt_1_1,.. txt_1_9 ...n
The textbox should be created like this
txt_0_0 txt_1_0 txt_2_0 txt_3_0 txt_4_0
txt_0_1 txt_1_1 txt_2_1 txt_3_1 txt_4_1
txt_0_2 txt_1_2 txt_2_2 txt_3_2 txt_4_2
txt_0_3 txt_1_3 txt_2_3 txt_3_3 txt_4_3
txt_0_4 txt_1_4 txt_2_4 txt_3_4 txt_4_4
then create a loop like this
string txtName = string.Empty;
int[,] aVal = new int[10, 10];
for (int i = 0; i < 10; i++)
{
for (int x = 0; x < 10; x++)
{
txtName = String.Format("txt_{0}_{1}", i, x);
string sVal = ((TextBox)this.Controls[txtName]).Text
int iVal = 0;
if (int.TryParse(sVal, out iVal))
aVal[i][x] = iVal;
}
}
Upvotes: 1