简体   繁体   中英

How do I change the text of text boxes in c# while storing references of them in an array or matrix?

Is there a way to change the text of textboxes in c# while storing references of them in an array ?

I've came across this problem while i was making a 9x9 sudoku game on C# widows form, I stored 81 text boxes in an array (tb) and then moved it to a matrix :

        tb[0] = form1.textBox1;
        tb[1] = form1.textBox2;
        tb[2] = form1.textBox3;
        tb[3] = form1.textBox4;
        tb[4] = form1.textBox5;
        tb[5] = form1.textBox6;
        tb[6] = form1.textBox7;
        tb[7] = form1.textBox8;
        tb[8] = form1.textBox9;
        tb[9] = form1.textBox10;
        tb[10] = form1.textBox11;
        tb[11] = form1.textBox12;
        .
        .
        .
        tb[80] = form1.textBox81;

moving the array (tb) to a matrix (tbmatrix) :

        TextBox[,] tbmatrix = new TextBox[9,9];
        int c = 0;
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                tbmatrix[i, j] = tb[c];
                c++;
            }
        }

I am going to put the random clues adding code just for if anyone wants to take a look :

    TextBox[] tb = new TextBox[81];
    Dictionary<int,string> col = new Dictionary<int,string>();
    Dictionary<int, string> row = new Dictionary<int, string>();
    Dictionary<Tuple<int,int>, string> box = new Dictionary<Tuple<int, int>, string>();
        Random initial = new Random();
        int index;
        int jindex;
        int val;
        int k = 0;
        while (k < 20)       //20 is the number of clues
        {
            index = initial.Next(-1, 9);      //a random number from 0-8
            jindex = initial.Next(-1, 9);     //same as above
            if (tbmatrix[index,jindex].Text != "")
            {
                continue;
            }
            t = new Tuple<int, int>(index/3, jindex/3);
            while (true)
            {
                val = initial.Next(0, 10);      //a random number from 1 to 9
                if(box[t].IndexOf(val.ToString()) == -1 && col[jindex].IndexOf(val.ToString()) == -1 && row[index].IndexOf(val.ToString()) == -1)
                {
                    break;
                }
            }
            tbmatrix[index, jindex].Text = val.ToString();
            tbmatrix[index, jindex].Enabled = false;
            k++;
        }

now here is the problem the last code doesnt change the actual text boxes it only changes the references of them... the only solution I've came across was to reassign the values to the original text boxes like something like that :

        form1.textBox1 = tb[0];
        form1.textBox2 = tb[1];
        form1.textBox3 = tb[2];
        form1.textBox4 = tb[3];
        .
        .
        .
        form1.textBox81 = tb[80]

So, any help ?

tbmatrix[index, jindex].Text = val.ToString();

Seems to work fine for me!

Also, change initial.Next(-1, 9); to initial.Next(0, 9); Because (-1, 9) generated a random int from -1 inclusive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM