2013-12-16 118 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