469,366 Members | 2,220 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

How to put a button click in an if statement?

Hi there, sorry if this is a really simple question.
I need to write code for:

If a button is pushed and then one variety of other buttons is pushed
then certain options on the program will become "greyed out". Ie they
can no longer be selected.

I assume i need to create an event from the first button being pushed
and then do if statements depending which button is pushed next

(IE. First Button is pushed,
If Button 2 is pushed then
buttons 6 and 8 become unusable
Else If Button 3 is pushed then
buttons 5 and 9 become unusable)

The problem i have is how do u put a button click in an if statement?
Please Please help.....this is driving me mad!
Kind Regards,
Code
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 21 '05 #1
7 39100
You might try something along the lines below where you name the buttons
But1, But2, etc:

Private Sub button_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles button1.Click, button2.Click, button3.Click, _
button4.Click, button5.Click, button6.Click
Select Case DirectCast(sender,button).Name
Case "but1"
button_Click (Me.but1, e)
Case "but2"
but5.Enabled=True:but9.Enabled=True
but6.Enabled=False:but6.Enabled=False
case "but3"
but5.Enabled=True:but6.Enabled=True
but5.Enabled=False:but9.Enabled=False
buttons 5 and 9 become unusable)
End Select
End Sub

"code100579" wrote:
Hi there, sorry if this is a really simple question.
I need to write code for:

If a button is pushed and then one variety of other buttons is pushed
then certain options on the program will become "greyed out". Ie they
can no longer be selected.

I assume i need to create an event from the first button being pushed
and then do if statements depending which button is pushed next

(IE. First Button is pushed,
If Button 2 is pushed then
buttons 6 and 8 become unusable
Else If Button 3 is pushed then
buttons 5 and 9 become unusable)

The problem i have is how do u put a button click in an if statement?
Please Please help.....this is driving me mad!
Kind Regards,
Code
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 21 '05 #2

"code100579" <ro********@hotmail-dot-com.no-spam.invalid> wrote in message
news:42********@127.0.0.1...
Hi there, sorry if this is a really simple question.
I need to write code for:

If a button is pushed and then one variety of other buttons is pushed
then certain options on the program will become "greyed out". Ie they
can no longer be selected.

I assume i need to create an event from the first button being pushed
and then do if statements depending which button is pushed next

(IE. First Button is pushed,
If Button 2 is pushed then
buttons 6 and 8 become unusable
Else If Button 3 is pushed then
buttons 5 and 9 become unusable)

The problem i have is how do u put a button click in an if statement?
Please Please help.....this is driving me mad!
Kind Regards,


OK, this may well not be the best way, but here are a couple of
possibilities...
From your description, buttons 2 and 3 have no functionality unless button 1
has been clicked, correct?
Disable buttons 2 and 3, and enable them in code only when button 1 is
clicked, then when the user clicks either 2 or 3, do as you've stated.
If buttons 2 and 3 *do* have associated functionality independent of button
1, consider making them fully independent and using, say, radio buttons
(enabled when the user clicks button 1) to allow the user make the choice
between which other buttons are enabled/disabled.
Or, you could use a class level variable to indicate that button one has
been clicked and test the variable in the event procedures for buttons 2 and
3. A Boolean would work well here. Set it in the event procedure for button
1, then be sure to housekeep the variable, once used, in the event
procedures for buttons 2 and 3.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 21 '05 #3
Hi,

Did you try the button's performclick() method?

if .....
button1.performclick()
end if

Bernie Yaeger

"code100579" <ro********@hotmail-dot-com.no-spam.invalid> wrote in message
news:42********@127.0.0.1...
Hi there, sorry if this is a really simple question.
I need to write code for:

If a button is pushed and then one variety of other buttons is pushed
then certain options on the program will become "greyed out". Ie they
can no longer be selected.

I assume i need to create an event from the first button being pushed
and then do if statements depending which button is pushed next

(IE. First Button is pushed,
If Button 2 is pushed then
buttons 6 and 8 become unusable
Else If Button 3 is pushed then
buttons 5 and 9 become unusable)

The problem i have is how do u put a button click in an if statement?
Please Please help.....this is driving me mad!
Kind Regards,
Code
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 21 '05 #4
> Private Sub button_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles button1.Click, button2.Click, button3.Click, _ button4.Click, button5.Click, button6.Click
Select Case DirectCast(sender,button).Name
Case "but1"
button_Click (Me.but1, e)


This will cause a stack overflow as you will continue to execute this
code over and over.

Nov 21 '05 #5
I suggest something similar to what Dennis suggested.

'Boolean variable to indicate if Button1 is pushed
Public bButton1Pushed As Boolean

Private Sub button_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles button1.Click, button2.Click, button3.Click,
_
button4.Click, button5.Click, button6.Click
Select Case DirectCast(sender,button).Name
Case "but1"
bButton1Pushed = True
Case "but2"
If bButton1Pushed Then
but6.Enabled = True
but8.Enabled = True
bButton1Pushed = False 'Reset the variable for the
next time
Else
'Code to run if Button1 wasn't pushed first
End If
case "but3"
If bButton1Pushed Then
but5.Enabled = True
but9.Enabled = True
bButton1Pushed = False 'Reset the variable for the
next time
Else
'Code to run if Button1 wasn't pushed first
End If
End Select
End Sub

Nov 21 '05 #6
> Private Sub button_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles button1.Click, button2.Click, button3.Click, _ button4.Click, button5.Click, button6.Click
Select Case DirectCast(sender,button).Name
Case "but1"
button_Click (Me.but1, e)


This will cause a stack overflow as you will continue to execute this
code over and over.

Nov 21 '05 #7
I suggest something similar to what Dennis suggested.

'Boolean variable to indicate if Button1 is pushed
Public bButton1Pushed As Boolean

Private Sub button_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles button1.Click, button2.Click, button3.Click,
_
button4.Click, button5.Click, button6.Click
Select Case DirectCast(sender,button).Name
Case "but1"
bButton1Pushed = True
Case "but2"
If bButton1Pushed Then
but6.Enabled = True
but8.Enabled = True
bButton1Pushed = False 'Reset the variable for the
next time
Else
'Code to run if Button1 wasn't pushed first
End If
case "but3"
If bButton1Pushed Then
but5.Enabled = True
but9.Enabled = True
bButton1Pushed = False 'Reset the variable for the
next time
Else
'Code to run if Button1 wasn't pushed first
End If
End Select
End Sub

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Philip Townsend | last post: by
reply views Thread by H Branyan | last post: by
2 posts views Thread by JCE | last post: by
5 posts views Thread by Antonio Policelli | last post: by
2 posts views Thread by mark | last post: by
4 posts views Thread by Dennis | last post: by
2 posts views Thread by Hrvoje Vrbanc | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.