Arash
Arash

Reputation: 3051

save the input value in a 2dimentsion array

how can i save the number that user enter in textbox in a 2 dimension array?

for example:

i have this numbers in textbox:45,78 and now i want to save 45,32 like this: array[0,0]=45 and array[0,1]=78

how can i do that?thanks,so much

edited: oh,when i entered 1,2,3,4,5,6,7,8,9 in textbox and it takes [2,2]=56

 private void button10_Click(object sender, EventArgs e)
    {
  int matrixDimention = 2;
        int[,] intValues = new int[matrixDimention + 1, matrixDimention + 1];
        string[] splitValues = textBox9.Text.Split(',');
        for (int i = 0; i < splitValues.Length; i++)
            intValues[i % (matrixDimention + 1), i % (matrixDimention + 1)] = Convert.ToInt32(splitValues[i]);
        string a=intValues[2,2].ToString();
        MessageBox.Show(a);

   }

when i take:

string a=intValues[2,1].ToString();

it shows 0

Upvotes: 2

Views: 1321

Answers (2)

Adriaan Stander
Adriaan Stander

Reputation: 166396

Have a look at using String.Split Method (Char[]) and Convert.ToInt32 Method (String)

Something like

string textBox = "45,78";
int[,] values = new int[1,2];
string[] textBoxSplit = textBox.Split(',');
values[0, 0] = Convert.ToInt32(textBoxSplit[0]);
values[0, 1] = Convert.ToInt32(textBoxSplit[1]);

EDIT

Example using List and Linq

string textBox = "45,78,1,2,3,4,5,6,7,8,9,10,11,12";
List<int> list = new List<int>(textBox.Split(',').Select(x => Convert.ToInt32(x)));

EDIT 2

Longwinded example using List and foreach

string textBox = "45,78,1,2,3,4,5,6,7,8,9,10,11,12";
List<int> list2 = new List<int>();
string[] splitVals = textBox.Split(',');
foreach (string splitVal in splitVals)
    list2.Add(Convert.ToInt32(splitVal));

EDIT

Enter the matrix

string textBox = "1,2,3,4,5,6,7,8,9";
int matrixDimention = 2;
int[,] intValues = new int[matrixDimention + 1, matrixDimention + 1];
string[] splitValues = textBox.Split(',');
for (int i = 0; i < splitValues.Length; i++)
    intValues[i/(matrixDimention + 1), i%(matrixDimention + 1)] = Convert.ToInt32(splitValues[i]);

EDIT

Follow the white rabbit

string textBox = "1,2,3,4,5,6,7,8,9";
int matrixDimention = 2;
int[,] intValues = new int[matrixDimention + 1, matrixDimention + 1];
string[] splitValues = textBox.Split(',');
for (int i = 0; i < splitValues.Length; i++)
    intValues[i/(matrixDimention + 1), i%(matrixDimention + 1)] = Convert.ToInt32(splitValues[i]);
string displayString = "";
for (int inner = 0; inner < intValues.GetLength(0); inner ++)
{
    for (int outer = 0; outer < intValues.GetLength(1); outer++)
        displayString += String.Format("{0}\t", intValues[inner, outer]);
    displayString += Environment.NewLine;
}
MessageBox.Show(displayString);

Upvotes: 2

TalentTuner
TalentTuner

Reputation: 17556

try this assuming array is a string array

str[] input = textBox.Text.Split(',');

 if(input.Length > 1)
  {
     arr[0,0] = input[0];

     arr[0,1]= input[1];

   }

Upvotes: 1

Related Questions