locked
IsEnabled problem

    Question

  • I try to create a simple Tic Tac Toe game for windows store app, and I have 9 button

    The buttons' name and alignment:

    A1   A2   A3

    B1   B2   B3

    C1   C2   C3

    And here is some of my coding(not yet complete the app):

    bool turn = true;//true=X,false=O //Button Click private void Button_Click(object sender, RoutedEventArgs e) { if (turn) { ((Button)sender).Content = "X"; ((Button)sender).IsEnabled = false; } else { ((Button)sender).Content = "O"; ((Button)sender).IsEnabled = false; } winningWay(); turn = !turn; } //winning Way private void winningWay() { winningFormula(A1, A2, A3); winningFormula(B1, B2, B3); winningFormula(C1, C2, C3); winningFormula(A1, B1, C1); winningFormula(A2, B2, C2); winningFormula(A3, B3, C3); winningFormula(A1, B2, C3); winningFormula(A3, B2, C1); } //winning formula private void winningFormula(Button button1, Button button2, Button button3) { if (button1.Content == button2.Content && button2.Content == button3.Content && button1.IsEnabled == false) { msgD(); } } //message Dialog private async void msgD() { var messageDialog = new MessageDialog("X Win!"); messageDialog.Commands.Add(new UICommand("Next round")); await messageDialog.ShowAsync(); }


    And my problem is since when I have 3 same content button clicked, like XXX or OOO, it doesn't show the message Dialog. I discovered that the problem might be the "button1.IsEnable==false", because if I try change the false to true, it show the message dialog when I pressing any button. So I want to ask why it work on "true" but won't work on"false"....or Is it a bug, I create a same Tic Tac Toe game for windows form before and it work perfect. And sorry for my bad English.

    • Moved by Mike Danes Thursday, March 6, 2014 12:20 PM winstore specific
    Thursday, March 6, 2014 11:55 AM

Answers

  • The Button.Content is of type object. So you can add anything into this button which has ContentPresenter.

    I don't know what you have added to this button but you can convert it into string and then compare it.

    private void winningFormula(Button button1, Button button2, Button button3)
            {
                if (button1.Content != null && button2.Content != null && button3.Content != null)
                    if (button1.Content.ToString() == button2.Content.ToString() && button2.Content.ToString() == button3.Content.ToString())
                    {
                        if (!button1.IsEnabled && !button2.IsEnabled && !button3.IsEnabled)
                            msgD();
                    }
            }

    I've tested it and it is working.

    If you face any issue then let me know.

    Friday, March 7, 2014 11:46 AM

All replies

  • Hi Chung

    I think the button1.IsEnable==false condition in the winningFormula method is not required. It is simply a useless.

    When three buttons are matched then why do you want to check whether a button is enabled or not.


    Thursday, March 6, 2014 12:27 PM
  • Hi Chung Wei Leong,

    It could not be a bug, lets consider about following situation.

    If the first user click A3, then winningFormula(A1, A2, A3) will not display the messageDialog, but winningFormula(B1, B2, B3) will display the messageDialog, because B1 and B2 and B3 content are "" (empty) now and B1 is enabled.

    I think this is not a API bug, but instead it could be a logic issue.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, March 6, 2014 12:35 PM
    Moderator
  • Hi Jamles,

    I know what you're said, but take a look the if condition from winningFormula(...),

    button1.Content==button2.Content && button2.Content==button3.Content && button1.IsEnabled==false)

    this is the condition that I want, the condition will only be true when the "3 buttons' content are same " and "button1 is untenable".....but the problem is it cannot work at all, so I try to change the IsEnabled from "false" to "true"(wish mean when I try to click a button it will show the message dialog, because the 3 button content is "" and button1 is enabled), after the testing, if IsEnable==true(testing) it work correctly, but if IsEnable==false(what I want) it will show nothing.

    I hope you can understand what I mean, and what you are said is when the IsEnabled==true =)


    Thursday, March 6, 2014 12:54 PM
  • Hi Jeetendra,

    if I don't use the button1.IsEnable==false it will show the message dialog every times I clicked one of the buttons, because others will have the same content "". 

    Thursday, March 6, 2014 12:59 PM
  • Now I have found that it is the ".Content" problem not the ".IsEnabled" problem.

    example: if(button1.Content ==button2.Content &&button2.Content ==button3.Content){...}

    if button1,2,3 all the Contents is "", the condition will be true.

    But if 3 of them have the same content "X","O", or anything, the condition will always get false.

    Anyone know other ways instead of ".content" word?

    Thursday, March 6, 2014 5:13 PM
  • When all three buttons have same content (X or O) then if(button1.Content ==button2.Content &&button2.Content ==button3.Content) condition will be true.

    Initially I didn't thought of having empty string in button's content and made the analysis. Well by just adding another condition after "if" statement will solve your problem.

    private void winningFormula(Button button1, Button button2, Button button3)
            {
                if (button1.Content == button2.Content && button2.Content == button3.Content)
                {
                    if (!button1.IsEnabled && !button2.IsEnabled && !button3.IsEnabled)
                        msgD();
                }
            }

    Friday, March 7, 2014 7:14 AM
  • I had tried it before but still the same issue.

    I also tried to set a break line there, and it just ignore the if(button1.Content ==button2.Content &&button2.Content ==button3.Content) condition, that's why I had discovered the .content problem.

    Now I'm using the .BorderThickness(To set difference thickness for X and O) instead of .Content to solve situation temporary.


    Friday, March 7, 2014 11:27 AM
  • The Button.Content is of type object. So you can add anything into this button which has ContentPresenter.

    I don't know what you have added to this button but you can convert it into string and then compare it.

    private void winningFormula(Button button1, Button button2, Button button3)
            {
                if (button1.Content != null && button2.Content != null && button3.Content != null)
                    if (button1.Content.ToString() == button2.Content.ToString() && button2.Content.ToString() == button3.Content.ToString())
                    {
                        if (!button1.IsEnabled && !button2.IsEnabled && !button3.IsEnabled)
                            msgD();
                    }
            }

    I've tested it and it is working.

    If you face any issue then let me know.

    Friday, March 7, 2014 11:46 AM
  • Thanks Jeetendra, it works for me!

    but one thing I want to ask about, is it the !button.IsEnabled meaning as false? Because I'm still beginner in programming=)


    Saturday, March 8, 2014 3:53 PM
  • The "!" is a negation operator. It returns true if operand is false and vice versa.

    In this case, if button is disabled then button.IsEnabled will be false. So we are checking all three buttons for they are disabled or not. Since button.IsEnabled returns false when button is disabled and therefore the if statement is evaluated to false and then the sub statements or if's next statement is not executed. So we negate it to make it true.

    I hope I tried my best to explain.

    Note: Please mark response as answer if they help.

    Saturday, March 8, 2014 8:19 PM
  • Thanks, looks like my problem already solved. 
    Sunday, March 9, 2014 2:30 AM