Problem with button text using IF statement!!!!!

Smug

New member
Joined
Jul 20, 2014
Messages
3
Programming Experience
Beginner
private void Vencer(object sender, EventArgs e)
{
    if ((bt1.Text == "O") && (bt2.Text == "O") && (bt3.Text == "O"))
    {
        MessageBox.Show("O Ganhou!");
    }
}

Even when this condition is true, the message box doesn't shows up, what am i doing wrong??
 
Last edited by a moderator:
Judging by the method signature, that's supposed to be an event handler. Has it actually been registered to handle the appropriate event? What event is is it supposed to be handling, because the name usually indicates that but it doesn't in this case? Did you write that method yourself or was it generated by the IDE?
 
It's not incorrect but just writing a method doesn't mean that the code it contains is going to be executed. The method has to actually be called. If you expect that method to be executed when a particular event is raised then you have to register it as a handler for that event.

To do that, open then designer, select the control or component whose event you want to handle, open the Properties window and click the Events button at the top. If you double-click an event without a handler, the IDE will generate an appropriate method and register it to handle the event. If you want to select an existing method then you can use the drop-down list to select from those methods with an appropriate signature.

If you want to see what the event handler registration looks like, open the designer code file for the form from the Solution Explorer. They will each look something like this:
someObject.SomeEvent += new EventHandler(SomeMethod);
 
On a different note, I wouldn't really be testing the Text of some Buttons to decide what to do. The Text on those Buttons is about presentation and should represent some underlying state. It's that underlying state that you should be testing and that should be represented by appropriate Boolean variables or the like. What does having an "O" on the Button mean? That should be represented in your code, not just on a control.
 
Back
Top Bottom