# Disable Button in VB 2008 when Using If..Then..Else Statement



## suvigyavijay (Jan 19, 2010)

This is what I have tried out:
-----------------------------------------------------------------------
Private Sub x1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles x1.Click
x1.Text = "X"
x1.Enabled = False
If x2.Text = "" And x3.Text = "" And x4.Text = "" And x5.Text = "" And x6.Text = "" And x7.Text = "" And x8.Text = "" And x9.Text = "" Then
x5.Text = "O"
x5.Enabled = False
Else x2.Text = "" And x3.Text = "X" And x4.Text = "" And x5.Text = "O" And x6.Text = "" And x7.Text = "" And x8.Text = "" And x9.Text = "" Then
x2.Text = "O"
x2.Enabled = False
End If
End Sub
-----------------------------------------------------------------------
Can Anyone help me. I am using VB Express Edition 2008 for making an application.


----------



## -Fabez- (Jul 28, 2008)

What is going wrong with the code ? A better explanation would be helpful


----------



## suvigyavijay (Jan 19, 2010)

Well I am making a Tic-Tac-Toe. The starting statement works out, it disables the button 'x1' and the Text changes to X. The code :
-------------------------------------------------------------
x1.Text = "X"
x1.Enabled = False
-----------------------------------------------------
But after using the if..then..else statement :
-----------------------------------------------------
If x2.Text = "" And x3.Text = "" And x4.Text = "" And x5.Text = "" And x6.Text = "" And x7.Text = "" And x8.Text = "" And x9.Text = "" Then
x5.Text = "O"
x5.Enabled = False
-----------------------------------------------------
The text of the button 'x5' changes to 'O' but the button 'x5' remains enabled. This means the command 'x5.Enabled = False' doesn't works out.
I am using Visual Basic Express Edition 2008 but I am not using VB.net.
If it is possible please give me your e-mail address I will mail you the file so that you can check for errors.

Thanks!


----------



## -Fabez- (Jul 28, 2008)

Try adding a message box or a debug output in the statement to ensure that all of your code is being executed. Using an If statement with lots of conditions can potentially lead to logical errors due to small mistakes or language features. I personally would use a generic handler for adding the text, checking if the user can select the current location, etc.


----------



## n00bSauce (Dec 15, 2009)

You might also try reversing the statements:

x5.Text = "O"
x5.Enabled = False

It would be interesting to see what the outcome was in this scenario... Just a thought.

Also, when you say that x5 is still enabled after the code is run, do you mean to say that you can actually click it? If so, try putting some garbage code (like a message box) in the click event for the button and see if you can call it.


----------



## suvigyavijay (Jan 19, 2010)

I have already tried out reversing the statements and all of the commands are being executed, I can't uderstand what the problem is.
If you can tell me how to put these conditions in 'Case Statement' then the problem will be solved.

Thanks!


----------



## -Fabez- (Jul 28, 2008)

The syntax for a Case statement can be found here. Why would you want to use a case statement ?


----------



## suvigyavijay (Jan 19, 2010)

I want to use case statement to get rid of this too many if..then..else statement. I think that by using case statement every command will be properly executed. But how to use it with too many conditions?


----------



## -Fabez- (Jul 28, 2008)

You can have as many conditions as you like with a Case statement, meaning you could include every combination you need to check. However I would advise that you use a generic event handler, which would be easier and less prone to errors.


----------



## suvigyavijay (Jan 19, 2010)

What is Generic Event Handler?


----------



## -Fabez- (Jul 28, 2008)

A generic event handler is a handler which can handle several different events. The code for each buttons event handler is very similar, which means it could be consolidated into a single generic event handler.


----------



## suvigyavijay (Jan 19, 2010)

Thanks I will reply you afterwards (maybe tommorrow). I hope the problem might be solved.


----------



## -Fabez- (Jul 28, 2008)

If you want I could create an example of a generic event handler for you ? Is there any particular reason why you are making Tic Tac Toe ?


----------



## suvigyavijay (Jan 19, 2010)

Yes, I have got a project for making any software in Visual Basic, so I decided to make a Tic Tac Toe. With making this I would too learn things that I haven't learned in my school.
I would like you to give me example for Generic Event Handler.
Thanks!


----------



## -Fabez- (Jul 28, 2008)

Below is an example of a generic event handler, which will perform the same code when both buttons are pressed.


```
AddHandler BtnExampleOne.Click, AddressOf ExampleFunction
AddHandler BtnExampleTwo.Click, AddressOf ExampleFunction

Function ExampleFunction(ByVal Sender As Object, ByVal EventArguments As System.EventArgs)
MsgBox("Example")
End Function
```


----------

