I can’t see the labels text’s. What should I do?
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();
}
}
}
In the last rows I convert nums to string and assign to texts. So I should see the texts but I didn’t. If I don’t use seq[] the code will be too long.
>Solution :
In the last rows I convert nums to string and assign to texts.
No, you absolutely do not. You never assign anything to the Text of any Label. Here:
string[] seq_Left = { lblNum1Left.Text, lblNum2Left.Text, lblNum3Left.Text, lblNum4Left.Text, lblNum5Left.Text };
You are getting the Text of each Label and putting it in a string array. That array just contains strings. It knows nothing about the Labels that those strings came from. That would be equivalent to this:
string str1 = lblNum1Left.Text;
// ...
string str5 = lblNum5Left.Text;
string[] seq_Left = { str1, str2, str3, str4, str5 };
Hopefully that makes it obvious why there’s no such connection. Here:
seq_Left[i] = numA.ToString();
you’re simply putting a string into the array, replacing the string that was there. The array knows nothing about any Labels so how could it affect them?
What you would need to do would be to create an array that actually contains the Labels:
Label[] seq_Left = { lblNum1Left, lblNum2Left, lblNum3Left, lblNum4Left, lblNum5Left };
and then set the Text of those Labels:
seq_Left[i].Text = numA.ToString();