【问题标题】:Progress Bar will not work, even when all the program does is show a Progress Bar进度条不起作用,即使程序所做的只是显示进度条
【发布时间】:2013-12-16 10:08:01
【问题描述】:

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

我已经尝试过 Marquee(我知道它在 Windows 8 中可能无法使用)和 Continuous,但我都无法使用。

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

Sub main()

    Dim ProgressForm As New formProgress
    ProgressForm.ShowDialog()

End Sub

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

其他信息

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

附加信息:跨线程操作无效:控件“proWorking”从创建它的线程以外的线程访问。

【问题讨论】:

  • 您是如何尝试从 BackgroundWorker 访问进度条的?您应该将 BGW 的 WorkerReportsProgress 属性设置为 True,然后使用 BGW.ReportProgress() 方法和 BGW.ProgressChanged 事件来更新进度条。
  • 您说得非常正确,这样做确实可以使“块”样式的进度条正常工作。我仍然觉得奇怪的是 Marquee 和 Continuous 都不起作用,即使是在一个实际上什么都没有发生的虚拟项目上。

标签: vb.net progress-bar


【解决方案1】:

如果您使用选取框样式,则必须将 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 的连续样式来停止它

【讨论】:

    【解决方案2】:

    要让进度条做某事(除了选取框样式),您需要设置 Value 属性。如果您有 .Minimum=0 和 .Maximum=100 则 .Value 为 50 意味着进度条是半满的。是否应该使用连续或块样式取决于视觉样式设置,并且在 Win 7 上并没有真正的区别(例如在 Win XP 下可能如此)。

    选取框样式意味着您不知道您的任务已经进行了多远。然后进度条显示一个连续移动的片段(仅在运行时可见!!)。我刚刚在 Win 7 中对其进行了测试,它可以工作。

    【讨论】:

      【解决方案3】:

      这是我与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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 2016-09-13
        相关资源
        最近更新 更多