为什么form_load显示命名空间无效(如果form_load提到btnCancel_Click之前就没事)
Imports Public Class Form1
Private numbertocomputer As Integer = 0
Private highestpercentagereached As Integer = 0
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As ) Handles BackgroundWorker1.DoWork
Dim worker As BackgroundWorker= CType(sender, BackgroundWorker)
e.Result = computerfibonacci(e.Argument, worker, e)
End Sub
Function computerfibonacci(ByVal n As Integer, ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs) As Long
If n < 0 OrElse n > 91 Then
Return -1
End If
Dim result As Long = 0
If worker.CancellationPending Then
e.Cancel = True
Else
If n < 2 Then
result = 1
Else
result = computerfibonacci(n - 1, worker, e) + computerfibonacci(n - 2, worker, e)
End If
Dim percentcomplete As Integer = CSng(n) / CSng(numbertocomputer) * 100
If numbertocomputer > highestpercentagereached Then
worker.ReportProgress(percentcomplete)
End If
End If
Return result
End Function
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As ) Handles BackgroundWorker1.RunWorkerCompleted
If (e.Error IsNot Nothing) Then
lblMessage.Text = e.Error.Message
ElseIf e.Cancelled Then
lblMessage.Text = "任务取消"
ElseIf e.Result = -1 Then
lblMessage.Text = "请输入0-99之间的数字"
Else
lblMessage.Text = "已完成!结果为" & e.Result.ToString()
End If
txbNum.Enabled = True
btnStart.Enabled = True
btnCancel.Enabled = False
ProgressBar1.Visible = False
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
txbNum.Enabled = False
btnStart.Enabled = False
btnCancel.Enabled = True
lblMessage.Text = "请稍等"
ProgressBar1.Visible = True
numbertocomputer = CInt(txbNum.Text)
BackgroundWorker1.RunWorkerAsync(numbertocomputer)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txbNum.Text = ""
ProgressBar1.Visible = False
btnCancel.Enabled = False
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.WorkerSupportsCancellation = True
End Sub
Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
BackgroundWorker1.CancelAsync()
btnCancel.Enabled = False
End Sub
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txbNum.Text = ""
ProgressBar1.Visible = False
btnCancel.Enabled = False
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.WorkerSupportsCancellation = True
End Sub