Jyreeil Jyreeil - 1 month ago 5
Vb.net Question

How do I make a section of code execute if a button is clicked after another button is clicked in visual basic

So basically what the title says. I want to make a program where you click a button, and then another button shows up, and then if you click the next button in a certain amount of time you get a point.
This is what I found in another thread, but this also makes the timer count down before the second button even shows up, even though this code is after the code making the next button show up.

Do While DoWhileBool = True
Select Case DirectCast(Sender, Button).Name
Case "ClickHere2"
If TimeCount > 0 Then
MultCount += 1
End If

Case "ClickHere3"
If TimeCount > 0 Then
MultCount += 1
End If

This is not the full code by any means, but I just wanted to show what I tried that doesn't work for having a button click event in an if statement inside another button click method.

Edit: I ended up figuring it out partially but pretty much all of what I was asking thanks to the help of the answer:

enter code here

Private Sub ClickHere1_Click(sender As System.Object, e As System.EventArgs) Handles ClickHere1.Click

If TimerVar <> 0 Then
MultCount += 1
MultCounter.Text = MultCount
End If
TimerVar = 5
'Do Until TimerVar = 0
' TimerVar = Timer1.ToString
' TimeCounter.Text = Timer1.ToString
' TimeCounter.Refresh()

End Sub

The commented section was where I was trying to get a textbox to show the countdown time, but it doesn't work. I'm sure I could figure it out if I wanted to, but I've moved on to other things. Thanks to the person who answered it, he probably led me to the right answer.


From what i understand of your question, this is how i would do it:

Add a timer, and 2 buttons to the form

On form load, you want to set the interval on the timer, so something like this:

Timer1.Interval = 1000       'Set the interval to 1 second

Then when you click on the first button show the second button, so on button1 click:

Button2.show()               'Show the second button
Timer1.Start()               'Start the timer, so they have 1 second from now

And in button 2 click, you want to do your event, add a point etc:

points += 1

Then to make the second button dissapear, (timeout) after a certian amount of time, you change the interval of the timer1. If the button wants to show for 1 second, set the interval to 1000 (milliseconds)

Then in timer1.tick add this code:

timer1.Stop()                'Stop the timer so that its not ran again and again
Button2.Hide()               'Hide the second button
MsgBox("You was too slow!!") 'Tell the user they missed it, or your code..