Public Event Working(ByVal Sender As Object, ByRef ok As Boolean)
Dim m_firsttime As Boolean = True
Dim m_cancel As Boolean
Dim m_ok As Boolean
Public Function Action() As Boolean
Me.ShowDialog()
Return m_ok
End Function
Public ReadOnly Property Cancel() As Boolean
Get
Return Me.m_cancel
End Get
End Property
Private Sub Form3_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
If m_firsttime Then
m_firsttime = False
Timer1.Enabled = True
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If MsgBox("取消吗?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Button1.Enabled = False
m_cancel = True
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
RaiseEvent Working(Me, m_ok)
Me.Close()
End Sub
抱歉,这两天去旅游了,现在帖出代码:
这个是主窗体FORM1上的调用和处理代码:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim f3 As New Form3()
AddHandler f3.Working, AddressOf Me.Working
MsgBox(f3.Action())
f3.Dispose()
End Sub
Private Sub Working(ByVal Sender As Object, ByRef ok As Boolean)
Dim f3 As Form3 = DirectCast(Sender, Form3)
Dim errmsg As String = ""
ok = True
With f3
ok = True
Try
Dim r As New Random()
Dim i As Integer, j As Integer
For i = 1 To 100
For j = 1 To 5000000
Next
If r.NextDouble < 0.005 Then
Throw New System.Exception("发生预设错误")
End If
.ProgressBar1.Value = i
.Label1.Text = "已完成" & i & "%"
Application.DoEvents()
If .Cancel Then
Exit For
End If
Next
Catch ex As System.Exception
ok = False
errmsg = ex.Message
End Try
ok = Not (.Cancel) And ok
If ok Then
MsgBox("成功", MsgBoxStyle.Information)
ElseIf .Cancel Then
MsgBox("取消", MsgBoxStyle.Exclamation)
Else
MsgBox(errmsg, MsgBoxStyle.Critical)
End If