0

我编写了一个“请稍候”表单,在其中插入了一个进度条。我环顾了如何编写进度条,这就是我所做的。首先,我对按钮进行了编程 - Button3_Click - 我按下开始。然后我对计时器进行了编程 - Timer1_Tick - 所以我写道:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Timer1.Enabled = True
    Timer1.Interval = 50
    Timer1.Start()
    
*[calculus code]*

If Form18.ProgressBar1.Value = 100 Then
    Form18.Hide()
    hrrspkexcel.Visible = True
End If

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
iProgressBarValue += 1
Select Case iProgressBarValue
    Case 1, 3, 5, 7, 9
        Form18.ProgressBar1.ForeColor = Color.White
        Form18.Label3.Text = Form18.ProgressBar1.Value & (" %")
        Form18.ProgressBar1.Value = (iProgressBarValue * 10)
    Case 2, 4, 6, 8
        Form18.Label3.Text = Form18.ProgressBar1.Value & (" %")
        Form18.ProgressBar1.Value = (iProgressBarValue * 10)
    Case 10
        Form18.ProgressBar1.Value = (iProgressBarValue * 10)
        Form18.Label3.Text = Form18.ProgressBar1.Value & (" %")
        Timer1.Stop()
        Timer1.Enabled = False
End Select
End Sub

我不明白为什么进度条卡在 100% 并且 form18 没有被隐藏,hrrspkexcel 也没有变得可见。我在哪里做错了?感谢您的任何支持。此致。

编辑:我试图编辑我的代码,因为评论说:

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Timer1.Enabled = True
        Timer1.Interval = 50
        Timer1.Start()
[calculus code]
        iProgressBarValue += 1
        Select Case iProgressBarValue
            Case 1, 3, 5, 7, 9
                Form18.ProgressBar1.ForeColor = Color.White
                Form18.Label3.Text = Form18.ProgressBar1.Value & (" %")
                Form18.ProgressBar1.Value = (iProgressBarValue * 10)
            Case 2, 4, 6, 8
                Form18.Label3.Text = Form18.ProgressBar1.Value & (" %")
                Form18.ProgressBar1.Value = (iProgressBarValue * 10)
            Case 10
                Form18.ProgressBar1.Value = (iProgressBarValue * 10)
                Form18.Label3.Text = Form18.ProgressBar1.Value & (" %")
                Timer1.Stop()
                Timer1.Enabled = False
        End Select
        If Form18.ProgressBar1.Value = 100 Then
            Form18.Hide()
            hrrspkexcel.Visible = True
        End If
End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Form18.ProgressBar1.Value = 100 Then
            Timer1.Stop()
            Timer1.Enabled = False
        End If
    End Sub

在这种情况下,进度条会停留在 10%。

编辑二:根据评论中的建议,我删除了计时器,并将我的进度条基于微积分代码的实体(Form18.ProgressBar1.Maximum)。无论如何,[演算代码]下报告的是一个繁重的 Excel 导出,因此进度条冻结为 0%,直到导出结束然后开始运行(我设置了加载光标以了解导出何时结束),所以也许我需要BackgroundWorker 让我的栏在导出时取得进展?(我是一个初学者程序员,我在某处读过一些关于这个的东西,但我不知道这个解决方案是否适合我,所以我在问)。

最后,这就是我更正代码的方式:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Form18.ProgressBar1.Minimum = 0
    Form18.ProgressBar1.Value = 0
    Form18.ProgressBar1.Maximum = [if loop criteria based on my code]
    Form18.ProgressBar1.Increment(1)
    Form18.Show()
    
    [calculus code - excel export]
    
    If Form18.ProgressBar1.Value = Form18.ProgressBar1.Maximum Then
        Form18.Hide()
        hrrspkexcel.Visible = True
    End If
End Sub

通过此编辑,即使导出已经结束,我的 ProgressBar 也会冻结在 0%,所以我显然在某处做错了。感谢您给予我的所有支持。


编辑 III:我设法使用 for 循环来增加进度条的值,正如你建议我的那样,使用与百分比的比例方程并克服关于条形最大值的问题,该最大值始终设置为 100。所以谢谢大家的支持。我想问你的最后一件事 - 如果我不是离题的话 - 是如何使我的加载表单 - 使用进度条 - 在前台并“锁定”与所有其他表单的交互。


编辑 III BIS:我尝试使用 Backgroundworker 来克服加载表单冻结。这是我第一次使用这个命令,我不知道它是否是让它与 ShowDialog 表单通信的正确方法。这是我写的:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Form18.ProgressBar1.Minimum = 0
    Form18.ProgressBar1.Value = 0
    BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Form18.ShowDialog()
[excel calculus export]
If Form18.ProgressBar1.Value = Form18.ProgressBar1.Maximum Then
    Form18.Hide()
    hrrnativexcel.Visible = True
End If
End Sub

我总是遇到同样的麻烦:当 Form18 出现时,仍然处于 0% 加载状态。


编辑 IV:对于以下情况,我遇到了关于进度条增量的问题:我有两个用于在 excel 中导出值的 for 循环,并且此循环的上限不同。所以我创建了第三个 for 循环,其上限是上述两个 for 循环的上限之和,以增加进度条进度。当它达到 100% 时,它会再次开始循环。我该如何解决这个问题?这是我正在使用的代码:

For i = 1 To CInt(Form6.ListBox1.Items.Count)
    For j = 1 To CInt(Form10.ListBox1.Items.Count)
        For k = 0 To CInt(CInt(Form6.ListBox1.Items.Count) + CInt(Form10.ListBox1.Items.Count))
            hrrspkexcel.Cells(j + 1, 1).value = Form10.ListBox2.Items(CInt(j + 1) - 2)
            hrrspkexcel.Cells(i + 1, 2).value = Form6.ListBox1.Items(CInt(i + 1) - 2)
            Form18.ProgressBar1.Value = CInt(100 * k / (CInt(Form6.Label9.Text) + CInt(Form10.ListBox1.Items.Count)))
            Form18.Label3.Text = Form18.ProgressBar1.Value & (" %")
        Next
    Next
Next

提前致谢。


编辑 V:我已根据注释更新了我的代码,以解决编辑 IV 中描述的问题。这是我写的:

Dim pbloop As Integer
pbloop = CInt(Form10.ListBox1.Items.Count) * CInt(Form6.ListBox1.Items.Count)
For p = 1 To pbloop
    For i = 1 To CInt(Form6.ListBox1.Items.Count)
        For j = 1 To CInt(Form10.ListBox1.Items.Count)
            hrrspkexcel.Cells(i + 1, 4).value = Form6.ListBox1.Items(CInt(i + 1) - 2)
            hrrspkexcel.Cells(i + 1, 3).value = Form6.ListBox2.Items(CInt(i + 1) - 2)
            hrrspkexcel.Cells(j + 1, 1).value = Form10.ListBox1.Items(CInt(j + 1) - 2)
            hrrspkexcel.Cells(j + 1, 2).value = Form10.ListBox2.Items(CInt(j + 1) - 2)
            Form18.ProgressBar1.Value = CInt(100 * p / pbloop)
            Form18.Label3.Text = Form18.ProgressBar1.Value & (" %")
        Next
    Next

我总是卡在 0% 并且进度条没有增加。

4

3 回答 3

1

您当前的代码看起来好像您希望在隐藏表单之前最多单击 10 次按钮时更新进度条。这是你的意图吗?我本来希望您希望“请稍候...”表单保持可见,进度条每隔一秒更新一次,然后消失?

为后者使用计时器很好,但对于前者,您可以直接更新进度条 - 正如您所做的那样 - 无需计时器。

此外,您不需要将进度条值乘以 10;您可以简单地将进度条Maximum值设置为 10。

此外,由于您在第一次单击时更改了ForeColor值,因此您可能可以省略Case Select {even|odd}测试,因为代码在其他方面是相同的。

因此,如果没有计时器或颜色变化以及使用 Progress Bar Maximum = 10,您需要做的就是:

Form18.ProgressBar1.Value = iProgressBarValue
Form18.Label3.Text = $"{iProgressBarValue} %")

不需要Case Select.

PB 的另一个选择是使用PerformStep()方法来增加它。Step对于这种方法,只需将属性设置为 1。

于 2020-11-21T23:07:46.050 回答
1

这不是问题的真正答案,而是向您展示如何使用进度条的示例。

这段代码并不漂亮,我知道,它是为了示例。当您使用文本框文本作为数字而不检查所写内容时,请小心。

在此处输入图像描述



Imports System.Threading

Public Class Form1


    Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click


        ' Initialisation of the progressbar
        myProgressbar.Value = 0
        myProgressbar.Maximum = tbxTo.Text

        lbxInfos.Items.Clear() ' Clear the content in the listbox1 if it's not the first run


        ' This simulate your [calculus code], I just add an information in the listbox
        For i As Integer = tbxFrom.Text To tbxTo.Text

            myProgressbar.Value = i ' Set the progressbar avancement

            lbxInfos.Items.Add("This is the loop n° " & i & " !")

            lbxInfos.Refresh() ' Just because the listbox will not be refresh until the end of the sub
            Thread.Sleep(200) ' The code is too fast, I add this to see the progress (= wait 200 ms)
        Next

        MsgBox("This is the end ! Now the form will be closed")

        Me.Close()

    End Sub



End Class

于 2020-11-22T11:46:19.693 回答
1

尝试更多类似的东西:

Private Async Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Button3.Enabled = False
    hrrspkexcel.Visible = False ' changed this to a Label next to the button just for demonstration

    Dim f18 As New Form18
    f18.ProgressBar1.Minimum = 0
    f18.ProgressBar1.Value = 0
    f18.ProgressBar1.Maximum = 100 ' <-- leave the maximum at 100!
    f18.Show(Me)

    Dim pct As Integer
    Dim maximum As Integer = 319 ' <-- figure out the max value based on your "calculus code"

    Await Task.Run(Sub()
                       ' [calculus code - excel export]
                       For i As Integer = 0 To maximum
                           System.Threading.Thread.Sleep(50) ' <- some kind of processing going on

                           ' calculate the percentage based on your loop value "i" and the "maximum":
                           pct = CDbl(i) / CDbl(maximum) * 100 ' <-- scale your loop value to 100
                           Try
                               f18.ProgressBar1.Invoke(Sub()
                                                           f18.ProgressBar1.Value = pct
                                                       End Sub)
                           Catch ex As Exception
                               ' User closed the progressform, create another one
                               Me.Invoke(Sub()
                                             f18 = New Form18
                                             f18.ProgressBar1.Minimum = 0
                                             f18.ProgressBar1.Value = 0
                                             f18.ProgressBar1.Maximum = 100 ' <-- leave it at 100
                                             f18.Show(Me)
                                             ' make progressbar immediately jump to the right spot
                                             f18.ProgressBar1.Value = f18.ProgressBar1.Maximum
                                             f18.ProgressBar1.Value = pct
                                         End Sub)
                           End Try
                       Next
                   End Sub)

    ' when we hit here, the "calculus code" above completed
    f18.Close()
    hrrspkexcel.Visible = True ' changed this to a Label next to the button just for demonstration
    Button3.Enabled = True
End Sub

这是它的实际效果:

在此处输入图像描述

于 2020-11-22T14:39:11.987 回答