标题:为什么form_load显示命名空间无效(如果form_load提到btnCancel_Click之前就 ...
只看楼主
fanhongwen
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-6-21
结帖率:0
已结贴  问题点数:0 回复次数:2 
为什么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
搜索更多相关主题的帖子: 空间 命名 Object 
2011-06-21 10:59
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:20 
End Class'注意这一行下面的代码,任意一个过程都不能出现在这个语句之外,楼主为啥写了两个Form1_Load?



===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2011-06-21 11:39
fanhongwen
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-6-21
得分:0 
回复 2楼 不说也罢
谢谢!  我是新手好多地方都没注意到....
2011-06-21 12:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-343525-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022983 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved