如果只匹配一个数字,如何使一个标签变为绿色

时间:2017-04-10 05:00:38

标签: c#

enter image description here

我是在右边,只有当我有3个匹配时才会做任何颜色

while (AttemptNumber <= 999)
{
    AttemptNumber = AttemptNumber + 1;   //ALT that i m not going to use : AttemptNumber++

    myString = "Attempt #" + AttemptNumber.ToString();

    myRandom1 = myRandomObj.Next(0, 9);
    myRandom2 = myRandomObj.Next(0, 9);
    myRandom3 = myRandomObj.Next(0, 9);

    random1Label.Text = myRandom1.ToString();
    random2Label.Text = myRandom2.ToString();
    random3Label. Text = myRandom3.ToString();

    AttemptsLbl.Text = AttemptNumber.ToString("#,##0");

    if (myRandom1 == myNum1 && myRandom2 == myNum2 && myRandom3 == myNum3)
    {
        // random label color changing for the label to green if matched
        random1Label.BackColor = Color.LightGreen;
        random2Label.BackColor = Color.LightGreen;
        random3Label.BackColor = Color.LightGreen;

        //we have match for all 3 digits = winner!
        myString = myString + " SUCCESS: " + myNum1 + myNum2 + myNum3 + " vs " + myRandom1 + myRandom2 + myRandom3;

        MessageBox.Show(" Match Found for All 3 Digits - it took " + AttemptNumber + " tries! ");

        Listbox.Items.Add(myString);

        matchFound = true;
        break;
    }
    else
    {
        //we dont have a match

        myString = myString + " Failed: " + myNum1 + myNum2 + myNum3 + " vs " + myRandom1 + myRandom2 + myRandom3;

        random1Label.BackColor = Color.LightGreen;
        random2Label.BackColor = Color.LightGreen;
        random3Label.BackColor = Color.LightGreen;

        Listbox.Items.Add(myString);      
    }

与pic显示器一样,​​我只希望匹配一种颜色的绿色,或者如果匹配两个数字则需要两种颜色。如果只匹配一个数字,如何使一个标签变为绿色? 如果只有一个标签与一个标签不匹配,如何使标签变为绿色?

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了你的问题。我想这就是你要找的东西。

  1. 每次迭代时将所有标签背景颜色重置为灰色
  2. 只有当该特定数字匹配时,才有条件地显示每个标签的背景。

     while (AttemptNumber <= 999)
    {
       //Reset the Background Color for every Iteration
      random1Label.BackColor = Color.Grey;//Initial Background color
       random2Label.BackColor = Color.Grey;//Initial Background color
       random3Label.BackColor = Color.Grey;//Initial Background color  
    
    
    
      AttemptNumber = AttemptNumber + 1;   //ALT that i m not going to use : AttemptNumber++
    
    myString = "Attempt #" + AttemptNumber.ToString();
    
        myRandom1 = myRandomObj.Next(0, 9);
        myRandom2 = myRandomObj.Next(0, 9);
        myRandom3 = myRandomObj.Next(0, 9);
    
        random1Label.Text = myRandom1.ToString();
        random2Label.Text = myRandom2.ToString();
        random3Label. Text = myRandom3.ToString();
    
        AttemptsLbl.Text = AttemptNumber.ToString("#,##0");
    
        if (myRandom1 == myNum1 && myRandom2 == myNum2 && myRandom3 == myNum3)
        {
    
            // random label color changing for the label to green if matched
    
           random1Label.BackColor = Color.LightGreen;
           random2Label.BackColor = Color.LightGreen;
           random3Label.BackColor = Color.LightGreen;
    
            //we have match for all 3 digits = winner!
    
            myString = myString + " SUCCESS: " + myNum1 + myNum2 + myNum3 + " vs " + myRandom1 + myRandom2 + myRandom3;
    
    
    
    
    
            MessageBox.Show(" Match Found for All 3 Digits - it took " + AttemptNumber + " tries! ");
    
    
            Listbox.Items.Add(myString);
    
            matchFound = true;
            break;
    
    
         }
             else
         {
    
            //we dont have a match
    
            myString = myString + " Failed: " + myNum1 + myNum2 + myNum3 + " vs " + myRandom1 + myRandom2 + myRandom3;
         if (myRandom1 == myNum1)
            random1Label.BackColor = Color.LightGreen;
         if (myRandom2 == myNum2)
             random2Label.BackColor = Color.LightGreen;
         if (myRandom3 == myNum3)
            random3Label.BackColor = Color.LightGreen;
    
           Listbox.Items.Add(myString);      
    
    
     }