积极答复者
关于ProgressBar控件使用的问题

问题
-
我想使用ProgressBar控件来显示对一个数组的初始化进度,编写了如下代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a(1000, 1000) As String
Dim I, J As Integer
ProgressBar1.Maximum = 1001
ProgressBar1.Minimum = 0
ProgressBar1.Value = 0
For I = 0 To 1000
For J = 0 To 1000
a(I, J) = "*****"
Next J
ProgressBar1.Increment(1)
Next I
MsgBox("初始化完毕")
End Sub期望数组初始化完毕,进度条填充到最右侧,然后显示消息框。可是为什么执行时,进度条还没填充完毕就开始显示消息框了呢。感觉MSGBOX提前出现了。
答案
-
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim a(1000, 1000) As String
Dim I, J As Integer
For I = 0 To 1000
For J = 0 To 1000
a(I, J) = "*****"
BackgroundWorker1.ReportProgress(1) '触发此事件,不要忘记把BackgroundWorker设置成WorkerReportsProgress=true.
Next J
Next I
End Sub- 已标记为答案 shellyqiu 2012年9月14日 12:23
全部回复
-
用BackgroundWorker控件
DoWork 初始化、ProgressChanged 显示进度、 RunWorkerCompleted显示消息框
- 已建议为答案 Youen ZenModerator 2012年9月10日 8:52
-
-
谢谢,对BackgroundWorker控件不甚了解,看了一下帮助,大概编写的一个,不成啊,能否给个详细的?
简单来说,处理Do_Work事件等;同时调用RunAsync()……
具体参考来自MSDN的示例代码:http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
using System; using System.ComponentModel; using System.Windows.Forms; namespace BackgroundWorkerSimple { public partial class Form1 : Form { public Form1() { InitializeComponent(); backgroundWorker1.WorkerReportsProgress = true; backgroundWorker1.WorkerSupportsCancellation = true; } private void startAsyncButton_Click(object sender, EventArgs e) { if (backgroundWorker1.IsBusy != true) { // Start the asynchronous operation. backgroundWorker1.RunWorkerAsync(); } } private void cancelAsyncButton_Click(object sender, EventArgs e) { if (backgroundWorker1.WorkerSupportsCancellation == true) { // Cancel the asynchronous operation. backgroundWorker1.CancelAsync(); } } // This event handler is where the time-consuming work is done. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; for (int i = 1; i <= 10; i++) { if (worker.CancellationPending == true) { e.Cancel = true; break; } else { // Perform a time consuming operation and report progress. System.Threading.Thread.Sleep(500); worker.ReportProgress(i * 10); } } } // This event handler updates the progress. private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { resultLabel.Text = (e.ProgressPercentage.ToString() + "%"); } // This event handler deals with the results of the background operation. private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled == true) { resultLabel.Text = "Canceled!"; } else if (e.Error != null) { resultLabel.Text = "Error: " + e.Error.Message; } else { resultLabel.Text = "Done!"; } } } }
- 已编辑 ThankfulHeartModerator 2012年9月11日 1:45
-
我在界面上放了一个Progressbar控件,设置其Maximum为1000000,另外还有一个命令按钮Button1和一个BackgroundWorker控件,代码如下,但是没效果,不懂了,帮忙看看,谢谢。
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End SubPrivate Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim a(1000, 1000) As String
Dim I, J As Integer
For I = 0 To 1000
For J = 0 To 1000
a(I, J) = "*****"
Next J
Next I
End SubPrivate Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Increment(1)
End SubPrivate Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MsgBox("初始化完毕")
End Sub
End Class -
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim a(1000, 1000) As String
Dim I, J As Integer
For I = 0 To 1000
For J = 0 To 1000
a(I, J) = "*****"
BackgroundWorker1.ReportProgress(1) '触发此事件,不要忘记把BackgroundWorker设置成WorkerReportsProgress=true.
Next J
Next I
End Sub- 已标记为答案 shellyqiu 2012年9月14日 12:23