2013-12-16 73 views
0

我有一种表格,目前除了打开外没有其他任何操作。表单有两个控件 - 一个“关闭”按钮和一个进度条。但是,当我打开表格时,我什么都没有。进度条只是坐在那里无所事事。即使所有程序都显示进度条,进度条也不起作用

我试过两个选取框(我理解可能无法在Windows 8中工作)和连续,但我无法得到任何地方。

这是我怎么显示在程序启动时的形式 -

Sub main() 

    Dim ProgressForm As New formProgress 
    ProgressForm.ShowDialog() 

End Sub 

及以下进度条的属性。我是否错过了能让这个酒吧工作的东西?谢谢。

enter image description here

其他信息

对于我的完整程序,我没有最初尝试使用座式的进度条,但我一直得到尝试更新从进度栏时出现以下错误一个BackgroundWorker。这就是为什么我试图让一个简单的选取框/连续条代替。

附加信息:跨线程操作无效:从其创建线程以外的线程访问控制'proWorking'。

+0

您是如何尝试从BackgroundWorker访问Progressbar的?您应该将BGW的WorkerReportsProgress属性设置为True,然后使用BGW.ReportProgress()方法和BGW.ProgressChanged事件一起更新进度条。 – Jens

+0

你是非常正确的,这样做的确能让'Block'风格的进度条起作用。我仍然觉得奇怪的是,即使在一个虚拟项目上,Marquee和Continuous都不起作用,甚至在没有任何事情发生的虚拟项目上也是如此。 –

回答

0

如果您使用选取框风格,你必须marqueeanimationspeed设置为某个值

// 
     // progressBar1 
     // 
     this.progressBar1.Location = new System.Drawing.Point(91, 118); 
     this.progressBar1.MarqueeAnimationSpeed = 50; 
     this.progressBar1.Name = "progressBar1"; 
     this.progressBar1.Size = new System.Drawing.Size(100, 23); 
     this.progressBar1.Style = System.Windows.Forms.ProgressBarStyle.Marquee; 
     this.progressBar1.TabIndex = 0; 

,并使用连续风格marqueeanimationsspeed 0来阻止它

0

对于进度从做一些事情(除了选取框样式)你需要设置Value属性。如果您有.Minimum = 0和.Maximum = 100,那么50的一个.Value表示进度条是半满的。如果你应该使用连续或块样式取决于视觉样式设置,并没有在Win 7上实现真正的区别(也许它例如在Win XP下)。

“选取框”样式意味着您不知道您的任务进行得有多远。进度条然后显示连续移动的部分(仅在运行时可见!)。我只是在Win 7中测试它,它可以工作。

-1

这是我用的BackgroundWorkerProgressBarLabel的一个小样板。

Public Class BackgroundWorkerUI 
Private args As New ProgressArgs 

    Private Sub bw_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles bw.DoWork 
    'set ProgressBar style to Marquee 
    args.Style = ProgressBarStyle.Marquee 
    bw.ReportProgress(0) 'triggers the progress changed event to update UI on correct thread 
    'some long operation 
    Threading.Thread.Sleep(5000) 


    'Set ProgressBar style to Continuous 
    args.Style = ProgressBarStyle.Continuous 

    For i As Integer = 0 To 100 

     If bw.CancellationPending Then 
     e.Cancel = True 
     Exit For 
     End If 

     args.Current = i 
     args.Max = 100 
     args.Status = String.Format("({0} of {1}) Updating...", args.Current, args.Max) 
     bw.ReportProgress(0) 

     'some operation 
     Threading.Thread.Sleep(100) 
    Next 

    End Sub 

Private Sub bw_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bw.ProgressChanged 
lblStatus.Text = args.Status 
If args.Style = ProgressBarStyle.Marquee Then 
    bar.Style = args.Style 
    bar.MarqueeAnimationSpeed = 15 
Else 
    bar.Style = ProgressBarStyle.Continuous 
    bar.Minimum = args.Min 
    bar.Maximum = args.Max 
    bar.Value = args.Current 
End If 
End Sub 

Private Sub bw_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted 
If e.Error IsNot Nothing Then 
    MessageBox.Show(e.Error.Message, "Background Worker Exception", MessageBoxButtons.OK, MessageBoxIcon.Error) 
Else 
    If e.Cancelled Then 
    lblStatus.Text = "Operation canceled" 
    Else 
    lblStatus.Text = "Done" 
    End If 
End If 
End Sub 


Private Sub BackgroundWorkerUI_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing 
If bw.IsBusy Then 

    Dim r = MessageBox.Show("A background process is still running. Are you sure you want to quit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) 

    If r = Windows.Forms.DialogResult.Yes Then 
    bw.CancelAsync() 
    End If 
    e.Cancel = True 
End If 
End Sub 

Private Class ProgressArgs 
Inherits EventArgs 

Public Property Status As String 
Public Property Current As Integer 
Public Property Min As Integer 
Public Property Max As Integer 
Public Property Style As ProgressBarStyle 

Public Sub New() 
    Status = "" 
    Current = 0 
    Min = 0 
    Max = 0 
    Style = ProgressBarStyle.Continuous 
End Sub 
End Class 
End Class