简体   繁体   English

如何在c#中更改文本框的文本,同时将它们的引用存储在数组或矩阵中?

[英]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 ? 有没有一种方法可以在将c#文本框的引用存储在数组中的同时更改其文本?

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 : 我在C#widows窗体上制作9x9数独游戏时遇到了这个问题,我在数组(tb)中存储了81个文本框,然后将其移动到矩阵中:

        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) : 将数组(tb)移动到矩阵(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); 另外,更改initial.Next(-1, 9); to initial.Next(0, 9); initial.Next(0, 9); Because (-1, 9) generated a random int from -1 inclusive 因为(-1,9)从-1(含)生成一个随机整数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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