简体   繁体   中英

How to make buttons backcolor different from eachother

How to make button2 Change its BackColor if its the same Color as in button1?

How does my program work? Well i have to press Start (button5) to mix my Colors, after that i press Check button to see the answer.

As you can see on the Picture, i have two green Buttons and two orange Buttons but I dont want any of the button.backcolors to be equal. They all supposed to be different.

In another words, if button2.backcolor is the same as button1.backcolor, its supposed to mix its backcolor until it gets different. The only Colors that im allowed to use are Green,Red,Blue,Purple,Orange,Yellow

Any ideas how can i achieve this?

在此处输入图片说明

Random random = new Random();

List<Color> possibleColors = new List<Color>()
{
    Color.Red,
    Color.Green,
    Color.Orange,
    Color.Blue,
    Color.Purple,
    Color.Yellow,       
};

private Color GetRandomColorOfLoist()
{
    return possibleColors[random.Next(0, possibleColors.Count)];
}

private void button5_Click(object sender, EventArgs e)
{
    button1.BackColor = GetRandomColorOfLoist();
    button2.BackColor = GetRandomColorOfLoist();
    button3.BackColor = GetRandomColorOfLoist();
    button4.BackColor = GetRandomColorOfLoist();
    button1.Visible = false;
    button2.Visible = false;
    button3.Visible = false;
    button4.Visible = false;
}

private void button6_Click(object sender, EventArgs e)
{
    button1.Visible = true;
    button2.Visible = true;
    button3.Visible = true;
    button4.Visible = true;
}

EDIT 1

Arul Manivannans idea worked but after pressing on start and on check, if i press start again, the game simply crashes. My last question is, how can i hide start button (button5) after clicking on check button (button6)?

My code:

  Random random = new Random();

   List<Color> possibleColors = new List<Color>()
{
    Color.Red,
    Color.Green,
    Color.Orange,
    Color.Blue,
    Color.Purple,
    Color.Yellow,

    };


        private Color GetRandomColorOfLoist()
        {

            int index = random.Next(0, possibleColors.Count);
            Color ColorToReturn = possibleColors[index];
            possibleColors.Remove(possibleColors[index]);

            return ColorToReturn;



        }

        private void button5_Click(object sender, EventArgs e)
        {
            button1.BackColor = GetRandomColorOfLoist();
            button2.BackColor = GetRandomColorOfLoist();
            button3.BackColor = GetRandomColorOfLoist();
            button4.BackColor = GetRandomColorOfLoist();
            button1.Visible = false;
            button2.Visible = false;
            button3.Visible = false;
            button4.Visible = false;

            List<Color> possibleColors = new List<Color>()
{
    Color.Red,
    Color.Green,
    Color.Orange,
    Color.Blue,
    Color.Purple,
    Color.Yellow,


        };
        }

        private void button6_Click(object sender, EventArgs e)
        {
            button1.Visible = true;
            button2.Visible = true;
            button3.Visible = true;
            button4.Visible = true;

        }

EDIT 2

Ok, i got it. Thanks for help

Just remove the Color returned from GetRandomColorOfLoist out of the possibleColors List. At start of button5_Click you have to refill the List of possible Colors again. Then it should work.

In order to, not to have a repeated color, how about removing the color item from the list?

     private Color GetRandomColorOfLoist()
      {
        int index = random.Next(0, possibleColors.Count);
        Color ColorToReturn = possibleColors[index];
        possibleColors.Remove(possibleColors[index]);

        return ColorToReturn;
      }

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