How to put a button click in an if statement?

  • Thread starter Thread starter code100579
  • Start date Start date
C

code100579

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
 
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 said:
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.
 
Hi,

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

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

Bernie Yaeger

code100579 said:
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
 
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.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top