สร้าง Background Worker ใช้งานกัน Progress Bar ค่ะ
โปรแกรมรันได้ปกติค่ะ แต่แถบไม่ขยับเลยค่ะ มันจะเด้งแถบเต็มทีเดียวตอนข้อมูลรันเสร็จแล้วค่ะ
โค้ด Formload Code (VB.NET)
BackgroundWorker1.WorkerReportsProgress = True
Code (VB.NET)
'โค้ด เมื่อกดปุ่ม Search ข้อมูลค่ะ
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Try
BackgroundWorker1.RunWorkerAsync()
If dtpFrom.Value.Date > dtpto.Value.Date Then MsgBox("Invalid Date !!") : Exit Sub
btnSearch.Enabled = False
'Process Data
SearchShift()
SearchNonWorkingday()
SearchIndividualNon()
If dtpFrom.Value.Date = dtpto.Value.Date Then
If dtpFrom.Value.Date = Date.Now.Date Then
SearchByDatenow()
Else
ShowData()
End If
Else
ShowData()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "HRMS", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim i As Integer
For i = 0 To 100 Step +1
'ถ้ามีการสั่ง Cancel ให้หยุดทันที
If BackgroundWorker1.CancellationPending = True Then
e.Cancel = True
Exit For
Else
'รายงานว่ามี prgress เพิ่ม 1 progress
BackgroundWorker1.ReportProgress(i)
System.Threading.Thread.Sleep(100)
End If
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
'LabelControl1.Text = e.ProgressPercentage
LabelControl1.Text = (e.ProgressPercentage.ToString() + "%")
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Cancelled = True Then
LabelControl1.Text = "Canceled!"
btnSearch.Enabled = True
ElseIf e.Error IsNot Nothing Then
LabelControl1.Text = "Error: " & e.Error.Message
btnSearch.Enabled = True
Else
LabelControl1.Text = "Done!"
btnSearch.Enabled = True
End If
End Sub
ขอบคุณค่ะ
Tag : .NET, Ms SQL Server 2005, VB.NET
Date :
2012-02-23 10:42:43
By :
ninja,aster
View :
1615
Reply :
3
No. 1
Guest
Code (VB.NET)
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
LabelControl1.Text = (e.ProgressPercentage.ToString() + "%")
LabelControl1.Refresh()
End Sub