Search code examples
c#wpftextbox

Textboxes and array C# WPF


I will have single-dimensional array, how can I fill all this textboxes with different index of this array?

For example:

int[] array1 = new int[] { 1, 2, 3 };
int[] array2 = new int[] { 4, 5, 6 };
int[] array3 = new int[] { 7, 8, 9 };

Textboxes

Anyone have any advice? Thank you


Solution

  • You can put your text boxes in an array:

    var boxes = new TextBox[,]
    {
         { textbox1, textbox2, textbox3 },
         { textbox4, textbox5, textbox6 },
         { textbox7, textbox8, textbox9 },
    };
    

    And your data too:

    var data = new int[][]
    {
        array1, array2, array3
    };
    

    And then set them in a loop:

    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
             boxes[i,j].Text = data[i][j].ToString();
    

    Note that, there are two different types of two dimensional arrays. First one represents a 3x3 array of text boxes, while the second one is actually a one-dimensional array of int[] values.