在再次启动代码之前等待 1 秒 - VB.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31982905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wait for 1 second before starting code again - VB.NET
提问by Bustin Jieber
I desperately need help with a game I am making. For a bit of context, i am making a memory game and i have the following piece of code that is being troublesome. I have a bunch of labels on the form, 16 to be exact, with 1 randomly generated symbol placed in each. Each symbol appears in the labels twice.
我正在制作的游戏急需帮助。对于一些上下文,我正在制作一个记忆游戏,并且我有以下一段代码很麻烦。我在表单上有一堆标签,准确地说是 16 个,每个标签中放置了 1 个随机生成的符号。每个符号在标签中出现两次。
------------------------------Continued----------------------------------------
'MsgBox("hello") 'used to check if the second inccorect press shows up - it does show but instantly changes colour
'''''''''''''''''NEED SOME CODE THAT PAUSES IT HERE'''''''''''''''
labels(0).ForeColor = Color.DarkRed
sender.ForeColor = Color.DarkRed
End If
flips = 1
End If
End If
tmrmemory.Enabled = True ' starts the timer after the user clicks the first label
End Sub
What's supposed to happen is that when the labels clicked don't match, it should show both the clicked labels for a short period before changing them both back to "DarkRed" which is the colour of the form's background.
应该发生的是,当点击的标签不匹配时,它应该在短时间内显示两个点击的标签,然后将它们都改回“DarkRed”,即表单背景的颜色。
I have tried using a timer but then i can't use sender.forecolor=color.darkredbecause it is not declared globally.
我曾尝试使用计时器,但后来无法使用,sender.forecolor=color.darkred因为它未在全局范围内声明。
I have also tried using the command Threading.Thread.Sleep(500)but it still doesn't show the second incorrect click. I know that the code i have used works because when i use the message box, i can see both symbols and when the two clicks are correct, it stays.
我也尝试过使用该命令,Threading.Thread.Sleep(500)但它仍然没有显示第二次不正确的点击。我知道我使用的代码有效,因为当我使用消息框时,我可以看到两个符号,并且当两次点击正确时,它会保持不变。
回答by Pradeep Kumar
Threading.Thread.Sleep(500)will actually pause your code for half a second. However during this time it won't do anything, not even refresh your controls. To get the effect you want, you need to call the YourControl.Refreshmethod before calling Threading.Thread.Sleepto force the control to redraw immediately.
Threading.Thread.Sleep(500)实际上会暂停您的代码半秒钟。但是在此期间它不会做任何事情,甚至不会刷新您的控件。要得到你想要的效果,需要在调用YourControl.Refresh前先调用方法Threading.Thread.Sleep,强制控件立即重绘。
On a side note, I would advise you not to call Threading.Thread.Sleepon UI thread. It will give a feeling of program hang. Instead do your work on a separate thread. You can either do all the work yourself right from creating a separate thread to destroying it, or use the BackgroundWorkercontrol which has all the functionality built in.
附带说明一下,我建议您不要调用Threading.Thread.SleepUI 线程。它会给人一种程序挂起的感觉。而是在单独的线程上完成您的工作。您可以自己完成从创建单独的线程到销毁它的所有工作,也可以使用BackgroundWorker具有所有内置功能的控件。
Here is the link to an article I wrote a long time ago regarding BackgroundWorker that might be useful for you:
这是我很久以前写的一篇关于 BackgroundWorker 的文章的链接,它可能对你有用:
http://www.vbforums.com/showthread.php?680130-Correct-way-to-use-the-BackgroundWorker
http://www.vbforums.com/showthread.php?680130-Correct-way-to-use-the-BackgroundWorker
回答by BobbyTables
Declare a variable outside the sub that stores what label should be flipped when the timer ends.
在 sub 之外声明一个变量,用于存储计时器结束时应该翻转的标签。
Label click sets
storedLabel = senderTimer tick sets storedLabel.ForeColor = Color.DarkRed
标签点击集
storedLabel = sender定时器滴答集 storedLabel.ForeColor = Color.DarkRed

