vb.net 如何通过单击按钮打破循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18338343/
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
How to break a loop with a single button click
提问by user2150279
I am using VB.NET and I have:
我正在使用 VB.NET,我有:
- a
Start
button - a
Stop
button - a
While
loop
- 一个
Start
按钮 - 一个
Stop
按钮 - 一个
While
循环
When the Start
button is pressed, the While
loop begins. I want to stop the loop when the Stop
button is pressed.
当Start
按下按钮时,While
循环开始。我想在Stop
按下按钮时停止循环。
I had tried to use Applications.DoEvents
, but the loop stops when the Stop
button is pressed twice.
我曾尝试使用Applications.DoEvents
,但当Stop
按下按钮两次时循环停止。
Below is my code using Applications.DoEvents
下面是我的代码使用 Applications.DoEvents
Dim stopclick As Boolean = False
Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
btnForward.Enabled = False
btnStop.Enabled = True
While
' Perform the while statements
If stopclick = True Then
stopclick = False
Application.DoEvents()
Exit While
End If
End While
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
stopClick = True
End Sub
- Have I used
Application.DoEvents
incorrectly? - What are the other options besides
Application.DoEvents
? How can I stop the loop with a singleStop
button click?
- 我用
Application.DoEvents
错了吗? - 除此之外还有哪些选择
Application.DoEvents
?如何通过Stop
单击按钮停止循环?
回答by Steven Doggart
You need to put the call to Application.DoEvents
outside of your If
statement, like this:
您需要将调用放在语句Application.DoEvents
之外If
,如下所示:
While True
Application.DoEvents()
' Perform the while statements
If stopclick Then
stopclick = False
Exit While
End If
End While
The Stop_Click
won't have a chance to be processed until you call DoEvents
, so, with the way you had it, stopClick
would never get set to True
.
在Stop_Click
您调用 之前不会有机会被处理DoEvents
,因此,按照您的方式,stopClick
永远不会设置为True
。
However, the larger issue, here, is that calling DoEvents
, at all, is very bad practice and can lead to some very difficult-to-fix bugs if you aren't very careful. It would be far better if the loop was performed in a background thread. Check out the BackgroundWorker
component for an easy to implement threading mechanism for WinForm projects. You'll find it in the tool box of your form designer.
然而,这里更大的问题是,调用DoEvents
是非常糟糕的做法,如果您不小心,可能会导致一些非常难以修复的错误。如果在后台线程中执行循环会好得多。查看BackgroundWorker
组件以获取易于实现的 WinForm 项目线程机制。您可以在表单设计器的工具箱中找到它。
Here's an example of how to do it with the BackgroundWorker
component:
以下是如何使用BackgroundWorker
组件执行此操作的示例:
Dim stopclick As Boolean = False
Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
btnForward.Enabled = False
btnStop.Enabled = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
stopClick = True
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
stopClick = False
While Not stopClick
' Perform the while statements
End While
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
btnForward.Enabled = True
btnStop.Enabled = False
End Sub
回答by Luke
You can simplify your while loop significantly by using stopClick as your condition:
您可以通过使用 stopClick 作为条件来显着简化 while 循环:
While Not stopClick
Application.DoEvents()
End While
回答by rogue5pawn
I know this is an old thread, but I found a simple workaround. Add a CheckBox to the form (it can even be "off the side of the form," if that made sense).
我知道这是一个旧线程,但我找到了一个简单的解决方法。向表单添加一个 CheckBox(如果有意义,它甚至可以“远离表单”)。
Private Sub btnStopLoop_Click(sender As System.Object, e As System.EventArgs) Handles btnStopLoop.Click
chkEndLoop.CheckState = CheckState.Checked
End Sub
Then, in the Loop you wish to exit:
然后,在您希望退出的循环中:
Do Until chkEndLoop.CheckState = CheckState.Checked
'Do Stuff Here
Loop
As long as the CheckBox.CheckState = CheckState.Unchecked
, the Loop will continue, but when the CheckBox becomes Checked, the Loop will Exit
只要CheckBox.CheckState = CheckState.Unchecked
,Loop 就会继续,但是当 CheckBox 变成 Checked 时,Loop 就会退出
回答by Linendra Soni
You have to use btnstop.focus()
at the start of the loop. This will be sure to solve your problem.
您必须btnstop.focus()
在循环开始时使用。这一定能解决您的问题。
Example:
例子:
Dim stopclick As Boolean = False
Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
btnForward.Enabled = False
btnStop.Enabled = True
btnStop.Focus() ' new code---------------------
While
' Perform the while statements
If stopclick = True Then
stopclick = False
Application.DoEvents()
Exit While
End If
End While
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
stopClick = True
End Sub