即使所有程序都显示进度条,进度条也不起作用

时间:2013-12-16 10:08:01

标签: vb.net progress-bar

我有一张表格,目前除了开场之外什么都不做。表单有2个控件 - 一个“关闭”按钮和一个进度条。但是,当我打开表格时,我什么都没得到。进度条只是坐在那里什么都不做。

我已经尝试过两个Marquee(我理解可能在Windows 8中不起作用)和Continuous,但是我无法使用它们。

这是我在程序启动时显示表单的方式 -

Sub main()

    Dim ProgressForm As New formProgress
    ProgressForm.ShowDialog()

End Sub

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

enter image description here

其他信息

对于我的完整程序,我最初尝试使用进度条的块样式,但在尝试从BackgroundWorker更新进度条时,我一直收到以下错误。这就是我试图让一个简单的Marquee / Continuous栏工作的原因。

  

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

3 个答案:

答案 0 :(得分: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来阻止它

答案 1 :(得分:0)

要使Progressbar执行某些操作(除了Marquee样式),您需要设置Value属性。如果你有.Minimum = 0和.Maximum = 100那么.Value为50就意味着Progressbar是半满的。如果你应该使用连续或块样式取决于视觉样式设置,并且在Win 7上没有真正的区别(例如它可能在Win XP下)。

Marquee风格意味着您不知道您的任务进展了多远。然后,Progressbar显示一个连续移动的部分(仅在运行时可见!!)。我刚刚在Win 7中对它进行了测试,它确实有效。

答案 2 :(得分:-1)

这是我使用的BackgroundWorkerProgressBar以及Label的小样板。

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