简体   繁体   中英

How to change colour of button whenever it is pressed?

To make sure that the player knows what difficulty they have chosen to proceed with onto my game, I want the current difficulty to be clearly indicated by the button's forecolour. Here's my code thus far that just changes the forecolour of each button whenever they're pressed:

public Form1()
    {
        InitializeComponent();
        MinimizeBox = false;
        MaximizeBox = false;

    }

    bool diffchosen = false;
    public static string difficulty;
    public static double multiplier;
    public static int lives;

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void easyButton_Click(object sender, EventArgs e)
    {
        this.ForeColor = Color.White;


        diffchosen = true;
        difficulty = "Easy";
        multiplier = 0.8;
        lives = 4;

    }

    private void mediumButton_Click(object sender, EventArgs e)
    {
        this.ForeColor = Color.White;


        diffchosen = true;
        difficulty = "Medium";
        multiplier = 1.0;
        lives = 3;


    }

    private void hardButton_Click(object sender, EventArgs e)
    {
        this.ForeColor = Color.White;

        diffchosen = true;
        difficulty = "Hard";
        multiplier = 1.5;
        lives = 2;


    }

    private void playButton_Click(object sender, EventArgs e)
    {
        if (diffchosen == false)
        {
            MessageBox.Show("Please choose a difficulty before proceeding");
        }
        else
        {
            Game_Screen game_Screen = new Game_Screen();

            this.Hide();
            game_Screen.Show();
        }
    }

But I had difficulty making sure that when the player decides to choose another option, the other 2 have the black forecolour property. I used this beforehand but it brought about issues like the fact that whenever I pressed a button, the Medium and Hard buttons decided to change their forecolour to white:

if (difficulty == "Hard" || difficulty = "Medium")
{
   this.ForeColour = Color.Black;       
}

How can I go about creating a loop that would change each button's forecolour depending on the user's choice of button?

Easiest might be just to create a toggle function eg

private void ToggleButton(int button)
{
    easyButton.ForeColor = Color.Black;
    mediumButton.ForeColor = Color.Black;
    hardButton.ForeColor = Color.Black;

    switch (button)
    {
        case 1: easyButton.ForeColor = Color.White; break;
        case 2: mediumButton.ForeColor = Color.White; break;
        case 3: hardButton.ForeColor = Color.White; break;
    }
}

and then on every click event you just call this function with the appropriate 'button number'

eg

private void easyButton_Click(object sender, EventArgs e)
{
    ToggleButton(1);
    ...
}

private void mediumButton_Click(object sender, EventArgs e)
{
    ToggleButton(2);
    ...
}

private void hardButton_Click(object sender, EventArgs e)
{
    ToggleButton(3);
    ...
}

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