简体   繁体   English

如果在visual basic中单击另一个按钮后单击一个按钮,如何执行一段代码

[英]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.无论如何,这不是完整的代码,但我只是想展示我尝试过的东西,它不适用于在另一个按钮单击方法内的 if 语句中具有按钮单击事件。

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.旁注:我不再使用visual basic,但我认为这是其中一部分的想法是点击游戏数字键盘输入和字母键输入的混合,并且输入会增加一段时间的乘数。 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在表单中添加一个计时器和 2 个按钮

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:然后当您单击第一个按钮时显示第二个按钮,因此单击 button1:

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:在按钮 2 单击中,您想要进行活动、添加点等:

points += 1

Then to make the second button dissapear, (timeout) after a certian amount of time, you change the interval of the timer1.然后要使第二个按钮消失(超时)一段时间后,您可以更改 timer1 的间隔。 If the button wants to show for 1 second, set the interval to 1000 (milliseconds)如果按钮要显示1秒,设置间隔为1000(毫秒)

Then in timer1.tick add this code:然后在 timer1.tick 中添加以下代码:

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Visual Basic中使用代码创建按钮? - How do i make a button in visual basic with a code? 在提示后单击“关闭”按钮时终止程序-Visual Basic - Terminate program when Close button is Clicked after Prompt - Visual Basic 每次在 Visual Basic 中单击按钮时,如何对按钮进行编码以增加/减小文本大小? - How to code buttons to increase/decrease text size everytime the button is clicked in Visual Basic? 在asp.net MVC5中,如何在单击视图的提交按钮后执行控制器代码? - In asp.net MVC5 how do we execute controller code after a View's submit button is clicked? 如何检查是否在vb.net中的另一个按钮代码中单击了按钮 - how to check if a button was clicked in another button code in vb.net 在asp.net(vb)的button_click()中单击另一个按钮后,如何使链接按钮可见 - How to make a link button visible after another button has been clicked in asp.net(vb) in button_click() Visual Basic MsgBox vbYesNo 单击“否”按钮时不接受 - Visual Basic MsgBox vbYesNo Doesn't accept when the 'No' button is clicked 根据单击的Visual Basic按钮更改文本框的输出 - Change output of textbox based on button clicked Visual Basic 如果单击另一个按钮,则显示按钮 - Show button if another button is clicked blockUI初学者,如何在单击按钮后使其运行? - blockUI first-timer, how can I make it run after a button has been clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM