请教如何循环中途停止
比如button1.click的事件如下dim a as integer
for i as integer=0 to 10000000
a+=i
end for
textbox1.text=a
如何单击button1后,循环没有结束前,button1.text显示为"终止",而且此时点击它时循环停下,输出此时的a的值到textbox1,求代码,多线程也行
[ 本帖最后由 mp654k 于 2011-7-31 13:32 编辑 ]
Public Class Form1 Dim Button1Count As Short = 0 Dim a As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Button1.Text = "开始" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click a = 0 For i As Integer = 0 To 10000000 a += 1 Application.DoEvents() If Button1Count Mod 2 = 1 Then Button1.Text = "开始" ElseIf Button1Count Mod 2 = 0 Then Button1.Text = "停止" TextBox1.Text = a Exit Sub End If Next TextBox1.Text = a Button1.Text = "开始" End Sub Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown Button1Count += 1 End Sub End Class
Public Class Form1 Dim Button1Count As Short = 0 Dim a As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Button1.Text = "Run" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Button1Count += 1 For i As Integer = 0 To 10000000 Application.DoEvents() If Button1Count Mod 2 = 1 Then Button1.Text = "" Button1.Text = "Stop" ElseIf Button1Count Mod 2 = 0 Then Button1.Text = "Run" TextBox1.Text = i Button1Count = 0 Exit For End If TextBox1.Text = i a = i Next TextBox1.Text = a End Sub End Class