heron
heron

Reputation: 3661

Textbox values into int array

My form looks like that:

http://content.screencast.com/users/TT13/folders/Jing/media/7689e48c-9bd6-4e22-b610-656b8d5dcaab/2012-07-06_0347.png

x, y, A,B,C are matrices. Texboxes right to x are named as x1,...,x6 and texboxes right to A are named as

a11,...,a16
...
a61, ... ,a66

All of them are ints. What I want to do is, to get this values into array like:

x=(20,...,756);

And A into 2d array like a[1][1]=932 ... a[6][6]=666.

If yes, how? with groupboxes? I can't figure out how to resolve this problem. Thx in advance

Upvotes: 2

Views: 3981

Answers (5)

Steve
Steve

Reputation: 216273

Seeing you have the control with names embedded with the matrix position we could write
No error checking here, I assume the numbers are always integers...

    int[] xMatrix = new int[6];
    int[,] aMatrix = new int[6,6];

    foreach (Control control in this.Controls) 
    { 
        if (control is TextBox) 
        { 
            string pos = control.Name.SubString(1);
            if(control.Name.StartsWith("a"))
            {
                int matrixPos = Convert.ToInt32(pos) ;
                int x = (matrixPos / 10) - 1;
                int y = (matrixPos % 10) - 1;
                aMatrix[x,y] = Convert.ToInt32(control.Text);
            }
            else if(control.Name.StartsWith("x")
            {
                int arrayPos = Convert.ToInt32(pos) - 1;
                xMatrix[arrayPos] =  Convert.ToInt32(control.Text);
            }
        } 
    } 

Upvotes: 3

Andre Calil
Andre Calil

Reputation: 7692

As you have fixed textboxes (that is, you are not creating them dynamically), you can go to your form definition and give a value to the Tag property.

Starting on the left of your form, it would look like this:

x.1 x.2 x.3 x.4 x.5 x.6
1.1 1.2 1.3 1.4 1.5 1.6
2.1 2.2 2.3 2.4 2.5 2.6
...

Then you would iterate through all textboxes retrieving it, like:

        string textboxMatrix;
        int textboxValue;

        foreach (Control control in this.Controls)
        {
            if (control is TextBox)
            {
                textboxMatrix = ((TextBox)control).Tag.ToString();
                textboxValue = Convert.ToInt32(((TextBox)control).Text);

                if (textboxMatrix.StartsWith("x"))
                {
                    int xPosition = Convert.ToInt32(textboxMatrix.Split('.')[1]);

                    x[xPosition] = textboxValue;
                }
                else
                {
                    int aX = Convert.ToInt32(textboxMatrix.Split('.')[0]);
                    int aY = Convert.ToInt32(textboxMatrix.Split('.')[1]);

                    A[aX, aY] = textboxValue;
                }
            }
        }

OR, with names (as you said):

            string textboxMatrix;
            int textboxValue;

            foreach (Control control in this.Controls)
            {
                if (control is TextBox)
                {
                    textboxMatrix = ((TextBox)control).Name;
                    textboxValue = Convert.ToInt32(((TextBox)control).Text);

                    if (textboxMatrix.StartsWith("x"))
                    {
                        int xPosition = Convert.ToInt32(textboxMatrix.ToCharArray()[1]);

                        x[xPosition] = textboxValue;
                    }
                    else
                    {
                        int aX = Convert.ToInt32(textboxMatrix.ToCharArray()[1]);
                        int aY = Convert.ToInt32(textboxMatrix.ToCharArray()[2]);

                        A[aX, aY] = textboxValue;
                    }
                }
            }

Upvotes: 0

3Pi
3Pi

Reputation: 1914

Can you not just create the arrays manually?

int[] X = new int[] {x1.Value,x2.Value,x3.Value,x4.Value,x5.Value,x6.Value };
int[,] A = new int{ {a11.Value, a12.Value, a13.Value, a14.Value, a15.Value, a16.Value },
                    {a21.Value, a22.Value, a23.Value, a24.Value, a25.Value, a26.Value },
                    {a31.Value, a32.Value, a33.Value, a34.Value, a35.Value, a36.Value },
                    {a41.Value, a42.Value, a43.Value, a44.Value, a45.Value, a46.Value },
                    {a51.Value, a52.Value, a53.Value, a54.Value, a55.Value, a56.Value },
                    {a61.Value, a62.Value, a63.Value, a64.Value, a65.Value, a66.Value } };

Though it doesn't implement any neat tricks, it is incredibly easy, and quick. And because the amount of text boxes are unlikely to change once you start using it, there should be no problem hard coding it like this.

Also, you should use a square array, [,] rather than a jagged array, [][] because a jagged array can have different lengths on each row, which is undesirable in a matrix, and declaring it as square will be clearer.

Upvotes: 0

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112299

Instead of placing the textboxes on the form manually using the designer, you could add them programmatically and at the same time store them in a matrix.

const int N = 6;

TextBox[,] _matrixATextBoxes = new TextBox[N, N];

public MyForm() // Form Constructor.
{
    InitializeComponent();
    SuspendLayout();
    int x = 50; // Horizontal position of first TextBox.
    for (int ix = 0; ix < N; ix++) {
        int y = 80; // Vertical position of first TextBox.

        for (int iy = 0; iy < N; iy++) {
            var tb = new TextBox();
            tb.Location = new Point(x, y);
            tb.Size = new Size(23, 40);
            _matrixATextBoxes[ix, iy] = tb;
            Controls.Add(tb);
            y += 30; // Vertical distance
        }
        x += 50; // Horizontal distance
    }
    ResumeLayout();
}

Now you can read the content easily with

int a[,] = new int[N, N];
for (int ix = 0; ix < N; ix++) {
    for (int iy = 0; iy < N; iy++) {
        int value;
        Int32.TryParse(matrixATextBoxes[ix, iy].Text, out value);
        a[ix, iy] = value;
    }
}

(Not tested.)

Upvotes: 1

fenix2222
fenix2222

Reputation: 4730

You can do something like this (assuming you have a parent control that wraps textboxes). In my case it is a asp:Panel control with id "pnlMain":

// Collection to hold your matrices
List<List<int>> myMatrices = new List<List<int>>();
// Iterate through all rows and columns
for (int i = 0; i <= 60; i = i + 10)
{
    var matrix = new List<int>();
    for (int j = 1; j <= 6; j++)
    {
        // Dynamically search parent control for child textboxes
        var txt = pnlMain.FindControl(string.Format("{0}{1}", i == 0 ? "x" : "a", i + j)) as TextBox;
        if (txt != null)
        {
            int value = 0;
            int.TryParse(int.Parse(txt), out value);
            matrix.Add(value);
        }
    }
    myMatrices.Add(matrix);
 }      

Upvotes: 1

Related Questions