注册 登录
编程论坛 VB.NET论坛

请教如何循环中途停止

mp654k 发布于 2011-07-31 13:31, 1375 次点击
比如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 编辑 ]
8 回复
#2
wube2011-07-31 15:29
dim Button1Count as short=0

Button MouseDown()
Button1Count+=1

Button1 Click()
dim a as integer
for i as integer=0 to 10000000
    a+=i
    appication.Doevents()
    if Button1Count mod 3=1 then Button1.text="Stop"
    if Button1Count mod 3=0 then exit For
end for
textbox1.text=a

[ 本帖最后由 wube 于 2011-7-31 15:32 编辑 ]
#3
mp654k2011-08-01 00:52
经测试,下面代码可以:
Public Class Form1
    Dim Button1Count As Short = 0
    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
        Button1Count += 1
        Dim a As Integer
        For i As Integer = 0 To 10000000
            a += 1
            Application.DoEvents()
            If Button1Count Mod 2 = 1 Then
                Button1.Text = "停止"
            Else
                Button1.Text = "开始"
                TextBox1.Text = a
                Exit Sub
            End If
        Next
        TextBox1.Text = a
        Button1.Text = "开始"
    End Sub
End Class
但这里a不是全局变量,换成全局变量时不能达到效果,我需要a是全局变量,继续请教楼上高手
#4
mp654k2011-08-01 01:06
如果a是全局变量,代码如下,最后显示出来始终是0,不明白为什么
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
        Button1Count += 1
        For i As Integer = 0 To 10000000
            a += 1
            Application.DoEvents()
            If Button1Count Mod 2 = 1 Then
                Button1.Text = "停止"
            Else
                Button1.Text = "开始"
                TextBox1.Text = a
                a = 0
                Exit Sub
            End If
        Next
        TextBox1.Text = a
        Button1.Text = "开始"
    End Sub
End Class
#5
wube2011-08-01 12:03
程序代码:

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


分开~循环还没跑完~应该不可能Click()又重头跑~要是真这样~a值就不对了~

P.S 我不是高手~我这是VB6中的思路~。NET中我也才刚学~

[ 本帖最后由 wube 于 2011-8-1 12:12 编辑 ]
#6
mp654k2011-08-01 12:58
试了,这样不行.第2次鼠标按下的时候确实停下了,也显示出来了此时的a值,但一松手就显示"1"了.
#7
mp654k2011-08-01 17:51
想学一下委托,请高手用委托解决
#8
wube2011-08-01 17:54
程序代码:

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
#9
mp654k2011-08-01 19:11
可是可以,不过比较勉强,其实用另外一个变量来保存a的值再赋给a也可以.但这只是我举的一个例子,实际情况比较复杂,我不希望用application.doevents这个方法.最好用委托解决,可惜不会,仍然非常感谢楼上高手.

[ 本帖最后由 mp654k 于 2011-8-1 19:13 编辑 ]
1