1

I am trying to add a ProgressBar to my program. The program basically compares two values of time and when the values are equal a MessageBox appears to indicate that time is up. I need the ProgressBar to load based on the time difference of the two values. One of the values in a clock and the other is input by the user (similar to an alarm).

My code:

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

Can anyone help me out i am really getting frustrated.....thanks

Basic
  • 26,321
  • 24
  • 115
  • 201
  • I'll have a look but as a point of clarification, there's no such thing as VB2012... There's VB.Net (.Net 2/3/3.5/4/4.5) and Visual Studio 2012 – Basic Aug 01 '14 at 22:56
  • You can see which version of the .Net framework you're using by right-clicking the project and going to Properties. Under the Application tab is the "Target Framework" drop-down. Let me know if you need any help with the answer below – Basic Aug 01 '14 at 23:13
  • I see you tried to edit my answer. It looks like the edit was rejected by other community members. If you need clarification please post a comment either on your question or my answer. If you need to show code examples/be more detailed, edit your question. Then I can have a look and we'll see if we can work out what the problem is. Of course, if it's a new question you should ask a new question using the button @ the top of the page. – Basic Aug 05 '14 at 07:14

1 Answers1

0

How about something like this...

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

See VB.NET Delegates and Invoke - can somebody explain these to me? for more information on Invoking.

Community
  • 1
  • 1
Basic
  • 26,321
  • 24
  • 115
  • 201