简体   繁体   中英

Disabling a button without using button.Enabled = False while only using ONE Click Event

So I'm designing a game in which you have to choose the larger number between two numbers in order to win the prize. There are five buttons (each has a random number given in it) and a button to reset

For the first time, Button4 (The 2nd from right)and Button 5(the right one) The number in Button5 will be shown and the number in Button4 will be hidden Like this: 1st round

If you guess it right, you will win the number you guess right

If you guess it wrong, the game is over and you will only win the amount number you guessed right in the previous round

For example (1st round: Button4 & Button5(8) +++ You choose Button5(8), it's correct, now you win 48 dollars 1st round

2nd round: Button3 & Button4(Shown, it turns out that it's 4) +++ You choose Button4 The game is over because the number in Button3 is 7, which is larger than 4 You only win 48 dollars (from the previous round) Like this: 2nd round

Note: Button1-Button3 are disabled before the program starts

QUESTION

What I wanna ask is: After the game is over, how do you make the button3 & button4 not be able to click again?? So, the only way to play the game is to click the button reset I know there's a method like this:

       button1.Click-= button1_Click(); //to disable the button

But I'm writing the codes using only ONE click event and after:

       MessageBox.Show("Game Over !!");

If I put sth. like:

       button5.Click-=button1_Click();
       button4.Click-=button1_Click();

It won't work well (there will be unwanted errors) because what I disable is the whole click event (Not only its subpart, which in this case is button4 & button5)

++Q: How to command it to only disable specific buttons inside ONE click event(Label1_Click)?

This is my code:

   public partial class Form1 : Form
    {  int[] rndm = new int[5];
   private void Form1_Load(object sender, EventArgs e)
    {
        reset(); //by andgand
    }
   private void button1_Click(object sender, EventArgs e)
    {
        int num1 = rndm[0];
        int num2 = rndm[1];
        int num3 = rndm[2];
        int num4 = rndm[3];
        int num5 = rndm[4];
        Button btnButton = (Button)sender;

        if (btnButton.Name == "button5")
        {   
            if (num4 > num5) //by andgand
            {
                button4.Text = "    " + rndm[3] + new string(' ', 6) + "可惜!!";
                MessageBox.Show("Game Over !!");

            }
            else if (num4 <= num5)
            {
                button5.Enabled = false;
                button5.Text = num5 + "";
                button4.Text = num4 + "";
                button3.Enabled = true;
                textBox1.Text = Convert.ToString(rndm[3])+Convert.ToString(rndm[4]);
            }}
       if (btnButton.Name == "button4")//by andgand
        {
            if (button4.Enabled == true & button5.Enabled == true)
                { if (num4 >= num5)
                {
                    button5.Enabled = false;
                    button5.Text = num5 + "";
                    button4.Text = num4 + "";
                    button3.Enabled = true;
                    textBox1.Text = Convert.ToString(rndm[3]) + Convert.ToString(rndm[4]);
                }
                else if(num4 < num5) //by andgand
                {
                    button4.Text = "    " + rndm[3] + new string(' ', 6) + "可惜!!";
                    MessageBox.Show("Game Over !!");

                }
            }
            else if (button3.Enabled == true & button4.Enabled == true)
            {
                if (num4 >= num3)
                {
                    button2.Enabled = true;
                    button4.Enabled = false;
                    button3.Text = num3 + "";
                    button3.Enabled = true;
                    textBox1.Text = Convert.ToString(rndm[2])+ Convert.ToString(rndm[3]) + Convert.ToString(rndm[4]);
                }
                else if (num4 < num3)
                {
                    button3.Text = "    " + rndm[2] + new string(' ', 6) + "可惜!!";
                    MessageBox.Show("Game Over !!");

                }
            }

     if (btnButton.Name == "button3")
      //similar as if it's button4,and so on
        }
    private void reset()
    {
        Random random = new Random();
        for (int i = 0; i < 5; i++)
        {
            rndm[i] = random.Next(0, 9);
        }
        button1.Enabled = false;
        button1.Text = "萬";
        button2.Enabled = false;
        button2.Text = "仟";
        button3.Enabled = false;
        button3.Text = "佰";
        button4.Enabled = true;
        button4.Text = "拾";
        button5.Enabled = true;
        button5.Text = rndm[4].ToString();
        textBox1.Text = Convert.ToString(rndm[4]);  
    }




     }}

A work-around could be to check if the game is over when you click any of the buttons. So, handle the event, but in case the condition for the game over is True, just return and do nothing.

Use a global variable isGameOver for this that saves the current status of the game ( false when it starts). You set it to true when game is over and you show that message. That's what you can use to validate later if the action of the buttons should be done or not.

Something like this:

private bool isGameOver = false;

private void button1_Click(object sender, EventArgs e)
{
    int num1 = rndm[0];
    int num2 = rndm[1];
    int num3 = rndm[2];
    int num4 = rndm[3];
    int num5 = rndm[4];
    Button btnButton = (Button)sender;

    if (isGameOver && (btnButton.Name == "button4" || btnButton.Name == "button5")) {
         return;
     } 

     ... // do other things

}

private void reset()
{
   isGameOver = false;
   ... // do other things

}

When the game is over, do:

            else if(num4 < num5) //by andgand
            {
                button4.Text = "    " + rndm[3] + new string(' ', 6) + "可惜!!";

                 isGameOver = true;
                 MessageBox.Show("Game Over !!");

            }

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