-1

I made a Tic Tac Toe game and I'm trying to add a few features. I was used to handle the taken buttons with

button.enabled=false;

The problems is that the text on the buttons turns grey.

So I made a button click for each button: A1_Click, A2_Click, and so on

This is my code into A1_click, and it's the same for the other buttons, the only thing that changes is "A1", "A2", and so on

        Button b = (Button)sender;            

        if (!A1.Text.Equals(""))
        {                
           MessageBox.Show("Not A Valid Input");

        }

I get "not a valid input" when I click a button I've already clicked before, I'd just like to be able to click an another button. I don't want to lose my turn if I click on an already taken button

Gino Perla
  • 91
  • 1
  • 10
  • 1
    I think you need to remove `string ctrlName = ((Button)sender).Name;` and `ctrlName = "A1";` and change the `if` statement to `if (!b.Text.Equals(""))...` – Sjips Nov 04 '14 at 12:44
  • this is just the name. A1 and b got the same value though. I still have the problem – Gino Perla Nov 04 '14 at 12:56
  • What is `A1.Text` when clicked? For example, show it (for debugging) in the messagebox.show: `MessageBox.Show("Button.Text is " + b.Text + ". Not A Valid Input");`. – Sjips Nov 04 '14 at 13:03
  • A1.Text = X b.Text = X It works, I get "not a valid input" when I click a button I've already clicked before, I'd just like to be able to click an another button. I just lose my turn if I click on a taken button – Gino Perla Nov 04 '14 at 13:06
  • When A1 (or b) text is `"X"`, then your `if` statement is essentially: `if (!"X".Equals(""))` and that evaluates always to `True`. Thus, the message is shown every time you are clicking on the button. – Sjips Nov 04 '14 at 13:12
  • Every time but the first time, when I click it for the first time, A1 is empty, so it's ok A1 to be empty, it's when the button is taken I want it to say "not a valid input cause the button is taken", do you have any idea about how to edit my code? I can't logically figure out how am I supposed to achieve that, I've also tried with a while loop – Gino Perla Nov 04 '14 at 13:17
  • 1
    What code do you have (as you say it) 'Take' a button? Please post this code as well. – Sjips Nov 04 '14 at 13:20

1 Answers1

1

Based on what I have read I think I know what you are looking for. You are looking for a way to force the user to lose a turn when they click a button that has already been taken, and you do not want to set the enabled property to false. If that is what you want, then I might have some code that could help. First if you are assigning the same click event to multiple buttons with different functions you should try something like this:

for (int i = 1; i < 10; i++)
{
     string currentButtonName = "A" + i;
     Control currentButton = this.Controls.Find(currentButtonName, true).FirstOrDefault();
     currentButton.Click += OnGameButton_Click;
}

What this is doing is searching your form for a control that has a specified name, and since your buttons have a similar name we can easily search for them. Then we can bind a specific function to all of them so that instead of 9 functions to modify you only have 1, and you can validate that they all work the same. Here is the OnGameButton_Click() event code:

private void OnGameButton_Click(object sender, EventArgs e)
{
    if (!hasGameStarted || shouldLoseTurn)
        return;

    // This is the current button user pressed
    Button b = (sender as Button);

    if (b.Name.Contains('A') && b.Enabled)
    {
        if (!b.Text.Equals(""))
        {
            MessageBox.Show("Not A Valid Input! You have lost your turn.");
            shouldLoseTurn = true;
        }
        else
        {
            b.Text = currentPlayersLetter;
            shouldLoseTurn = false;
        }
    }
}

As you can see with some flags we can monitor the game, and force the buttons to react accordingly take a look at the beginning of the function. We have to validate that the game is engaged, and that the current user has not lost their turn due to pressing the same button. From there we just need modify what you had so that if they do press the same button twice then we modify the shouldLoseTurn flag as needed.

The other approach is to just simply use the Button.Enabled property to disable the button from use. I know you do not what the button to be grayed out, but if you create your own style guide for the button you could make it how you want. This can be challenging though because you will have to modify the default style template for the button to achieve this. Here is another question that discusses just that here

Community
  • 1
  • 1
Ckrempp
  • 324
  • 2
  • 9