11 เมษายน 2555

การใช้งาน Progress Bar VB.Net

   
Imports system.ComponentModel
Public Class Form1

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Try
            BackgroundWorker1.RunWorkerAsync()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Try

            Dim bgw As BackgroundWorker = TryCast(sender, BackgroundWorker)
            Application.CurrentCulture = New System.Globalization.CultureInfo("en-GB")
            Dim intupdate As Integer = 0
            Dim max As Double = 100000
            For i As Integer = 0 To max
                'ถ้ามีการ Cancel ให้ยกเลิก
                If Me.BackgroundWorker1.CancellationPending = True Then
                    Me.BackgroundWorker1.CancelAsync()
                    Exit Sub
                End If
                intupdate = CInt((i / max) * 100)
                If intupdate > 12 Then
                    Dim asss As String = ""
                End If
                Me.ShowProgress(intupdate, bgw, e)
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Function ShowProgress(ByVal Pvalue As Integer, ByVal wk As BackgroundWorker, ByVal e As DoWorkEventArgs) As Boolean
        Try
            If Not wk.CancellationPending Then
                If Pvalue >= 100 Then Pvalue = 100
                wk.ReportProgress(Pvalue)
                Return True
            Else
                wk.ReportProgress(0)
                Return False
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Try
            ProgressBar1.Value = e.ProgressPercentage
            Label1.Text = "ระบบกำลังประมวลผล... " & CStr(e.ProgressPercentage) & "%"
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Try
            If e.Error Is Nothing Then
                ProgressBar1.Value = 0
                Label1.Text = "ประมวลผลเรียบร้อย"
                MessageBox.Show(" Process End Time : [" & Now.ToString() & "]")
            Else
                MsgBox(String.Format("Work fail with error: {0}", e.Error.Message))
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            If Me.BackgroundWorker1.CancellationPending = False Then
                Me.BackgroundWorker1.CancelAsync()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

End Class

0 ความคิดเห็น:

แสดงความคิดเห็น