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

StackOverflow https://stackoverflow.com/questions/23127452

  •  05-07-2023
  •  | 
  •  

質問

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:

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

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.

Sidenote: I don't use visual basic anymore, but the idea I had that this was a part of was sort of a mix of clicker game num pad typing and letter key typing and the typing would increase a multiplier for a while. Never really finished that idea and I don't even know if what I had made in that game even exists anymore because my external hard drive went kaput before I had transferred all the old files onto my current computer.

役に立ちましたか?

解決

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..
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top