Click here to Skip to main content
15,914,395 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 100 textboxes in my form and I want to determine my textboxes' text to a 10*10 matrix.

How can I do this with for loops?

I mean for example

C#
for (int i = 0; i < 100; i++){
           {
               for (int j = 0; j < 100; j++)
                   matrix[i, j] = float.Parse(TextBox[i,j].text);
           }  


but c# dosn't have somthing like this, please help me:confused:
Posted
Updated 20-Jun-10 5:45am
v2

You can try something like this. Set your textbox id something like this textBox_<row>_<column> like textBox_1_1, textBox_1_2

and your code will look like this.

for (int i = 0; i < 10; i++){
          {
              for (int j = 0; j < 10; j++)
                  matrix[i, j] =( (TextBox) FindControl(string.Format("textBox_{0}_{1}", i, j))).Text;
          }




You can also store the id of the textboxes against their row column index in a collection and fetch the id from the collection when required.
 
Share this answer
 
Try this; i just knocked this up in C# and works fine (learn something new everyday!)

create a form with 2 buttons on it; One does the create, and one does the output to debug window;

This is the code to create 100 textboxes named TextBox0 to TextBox99 and set their text to a value from 0 to 99 and also the matrix class variable. It will then search the forms controls collectin for each textbox and load its value to the matrix.
C#
//Create a new matrix for the textbox values
private int[,] matrix = new int[10, 10];

public void create100textboxes()
{
    // Create 100 textboxes, set text to the textbox number
    for (int i = 0; i < 100; i++)
    {
        TextBox newTextbox = new TextBox();
        newTextbox.Name = "TextBox" + i.ToString();
        newTextbox.Text = i.ToString ();

        this.Controls.Add(newTextbox);
        newTextbox.BringToFront();
    }

    int textBoxCount = 0;
    //Create the row loop
    for (int r = 0; r < 10; r++)
    {
        //Create the col loop
        for (int c = 0; c < 10; c++)
        {

            TextBox currentTextBox = (TextBox) Controls[Controls.IndexOfKey("TextBox" + textBoxCount.ToString())];

            matrix[c,r] = int.Parse(currentTextBox.Text);
            textBoxCount++;
        }
    }
}


The code below will iterate through the matrix and output to the debug window each row/col address;
C#
private void buttonPrint_Click(object sender, EventArgs e)
{
    for (int r = 0; r < 10; r++)
    {
        for (int c = 0; c < 10; c++)
        {
            System.Diagnostics.Debug.Print("Matrix[" + r + "," + c + "]=" + matrix[r, c]);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900