【问题标题】:Issue with Progressbar in VB2012VB 2012 中的进度条问题
【发布时间】:2014-08-02 07:10:55
【问题描述】:

我正在尝试将 ProgressBar 添加到我的程序中。该程序基本上比较两个时间值,当值相等时,会出现一个 MessageBox 以指示时间到了。我需要根据两个值的时间差来加载 ProgressBar。时钟中的一个值,另一个由用户输入(类似于警报)。

我的代码:

Imports System.Net.Mime.MediaTypeNames

Public Class Form1
    Private hour As Integer = 0
    Private minute As Integer = 0
    Private second As Integer = 0

    Public Sub show_time()
        second += 1
        If second = 59 Then
            second = 0
            minute += 1
            If minute = 59 Then
                minute += 1
                hour += 1
            End If
        End If

        Label3PrgressStdPC.Text = hour.ToString.PadLeft(2, "0") & ":"
        Label3PrgressStdPC.Text &= minute.ToString.PadLeft(2, "0") & ":"
        Label3PrgressStdPC.Text &= second.ToString.PadLeft(2, "0")
        Label3PrgressStdPC.Refresh()

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        show_time()
        If TextBox1.SelectedText = TextBox1.Text Then Exit Sub
        If TextBox1.Text = Label3PrgressStdPC.Text Then
            Timer1.Stop()
            MsgBox("time is up")

        End If
    End Sub

    Private Sub Bn_start_St01_Click(sender As Object, e As EventArgs) Handles Bn_start_St01.Click
        Timer1.Start()
        Timer1.Enabled = True
        Timer2.Start()
        Timer2.Enabled = True

    End Sub


    **Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click
        ProgressBar1.Maximum = , the max progrssbr will be determine by user input
        ProgressBar1.Minimum = 0**

    End Sub



    **Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
       progresbar1.value = ,Not so sure how to write the logic here**

    End Sub
End Class

谁能帮帮我,我真的很沮丧.....谢谢

【问题讨论】:

  • 我会看看,但为了澄清一点,没有 VB2012 这样的东西......有 VB.Net (.Net 2/3/3.5/4/4.5) 和 Visual Studio 2012
  • 您可以通过右键单击项目并转到“属性”来查看正在使用的 .Net 框架的版本。在应用程序选项卡下是“目标框架”下拉菜单。如果您需要以下答案的任何帮助,请告诉我
  • 我看到你试图编辑我的答案。看起来编辑被其他社区成员拒绝了。如果您需要澄清,请对您的问题或我的回答发表评论。如果您需要显示代码示例/更详细,请编辑您的问题。然后我可以看一下,我们会看看我们是否可以解决问题所在。当然,如果是新问题,您应该使用页面顶部的按钮 @ 提出新问题。

标签: vb.net alarm


【解决方案1】:

这样的事情怎么样...

Private Ticker As Timer = New Timer 'Create a timer
Private Start As DateTime 'Store when we start
Private Expire As DateTime 'and when we end

'Call this to get things going
Sub Begin(EndHour As Integer, EndMinute As Integer, EndSecond As Integer)
    Start = DateTime.Now

    'If input is a time today ...
    Expire = DateTime.Now.Date.Add(New TimeSpan(EndHour, EndMinute, EndSecond))

    'or just a number of hours/mins/secs from now...
    Expire = DateTime.Now.Add(New TimeSpan(EndHour, EndMinute, EndSecond))

    'When the timer fires, call Tick()
    AddHandler Ticker.Elapsed, Sub() Tick()

    Ticker.Enabled = True
    Ticker.Interval = 1000
    Ticker.Start

End Sub

Private Sub Tick()
    If DateTime.Now < Expire Then
        'Not Finished
        Dim Elapsed = DateTime.Now.Subtract(Start)
        Dim TotalMillis = Expire.Subtract(Start).TotalMilliseconds
        Dim ProgressDouble = Elapsed.TotalMilliseconds / TotalMillis

        'Me.Invoke is used here as the timer Tick() occurs on a different thread to the
        'one used to create the UI. This passes a message to the UI telling it to
        'update the progress bar. 
        Me.Invoke(Sub() 
                      ProgressBar1.Value = CInt(ProgressDouble * ProgressBar1.Maximum)
                      Label3PrgressStdPC.Text = Elapsed.ToString
                  End Sub)
    Else
        'Done
        MessageBox.Show("Done")
        Ticker.Stop
    End If
End Sub

有关调用的更多信息,请参阅VB.NET Delegates and Invoke - can somebody explain these to me?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-18
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多