winforms 我看不到标签和文字,我该怎么办?

ztmd8pv5  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(114)

我看不到标签文本。我该怎么办?


的数据

public partial class Form1 : Form
    {

        Random rnd = new Random();
        string[] Maths = { "Add", "Subtract", "Multiply" };
        int[] total = new int[5];
        int score = 0;

        public Form1()
        {
            InitializeComponent();
            SetUpGame();
        }

        private void SetUpGame()
        {
            string[] seq_Left = { lblNum1Left.Text, lblNum2Left.Text, lblNum3Left.Text, lblNum4Left.Text, lblNum5Left.Text };
            string[] seq_Right = { lblNum1Right.Text, lblNum2Right.Text, lblNum3Right.Text, lblNum4Right.Text, lblNum5Right.Text };
            string[] seq_Symbol = { lblSymbol1.Text, lblSymbol2.Text, lblSymbol3.Text, lblSymbol4.Text, lblSymbol5.Text };
            string[] seq_Answer = { txtAnswer1.Text, txtAnswer2.Text, txtAnswer3.Text, txtAnswer4.Text, txtAnswer5.Text };

            for (int i = 0; i < 5; i++)
            {
                int numA = rnd.Next(10, 20);
                int numB = rnd.Next(0, 9);

                seq_Answer[i] = "";

                switch (Maths[rnd.Next(0, Maths.Length)])
                {
                    case "Add":
                        total[i] = numA + numB;
                        seq_Symbol[i] = "+";
                        break;

                    case "Subtract":
                        total[i] = numA - numB;
                        seq_Symbol[i] = "-";
                        break;

                    case "Multiply":
                        total[i] = numA * numB;
                        seq_Symbol[i] = "x";
                        break;
                }

                seq_Left[i] = numA.ToString();
                seq_Right[i] = numB.ToString();
            }
        }
    }

字符串
在最后几行中,我将nums转换为string并赋值给文本。所以我应该看到文本,但我没有。如果我不使用seq[],代码会太长。

k5hmc34c

k5hmc34c1#

在最后几行中,我将数字转换为字符串并分配给文本。
不,你绝对不能。你永远不能给任何LabelText分配任何东西。这里:

string[] seq_Left = { lblNum1Left.Text, lblNum2Left.Text, lblNum3Left.Text, lblNum4Left.Text, lblNum5Left.Text };

字符串
你得到每个LabelText,并把它放在一个string数组中。这个数组只包含strings。它不知道这些strings来自的Labels。这相当于:

string str1 = lblNum1Left.Text;
// ...
string str5 = lblNum5Left.Text;

string[] seq_Left = { str1, str2, str3, str4, str5 };


希望这能解释为什么没有这种联系。这里:

seq_Left[i] = numA.ToString();


你只是把一个string放进数组,替换掉原来的string。数组对任何Labels都一无所知,所以它怎么会影响它们呢?
你需要做的是创建一个实际包含Labels的数组:

Label[] seq_Left = { lblNum1Left, lblNum2Left, lblNum3Left, lblNum4Left, lblNum5Left };


然后设置这些Labels中的Text

seq_Left[i].Text = numA.ToString();

相关问题