none
Timer Interval Display in VB.NET? RRS feed

  • Question

  • Hello. i have been using the Visual Studio Community 2017 Program for a few months. i have desided to make a virtual Operating System (Fullscreen, apps, files. like a application (.exe) that has all of a OS like windows.). i would like to create a [fake] Battery System, which i already have, however i would like to display the interval returning from the timer.

    example:

    so their is one timer, and the timer makes a progress bars value change. when it hits 0, it closes the program. but i would like to make a label that shows this: (00 is a varible for the progressbar value left) (1111 is a value for the interval that has passed on the timer) 00 . 1111 % or 00.1111%

    Example: 53 . 627361 % or 53.627361%

    Thank you.

    here is my current code for when the timer ticks:

        Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
            ProgressBar1.Value = ProgressBar1.Value - 1
            If ProgressBar1.Value = 10 Then
                MessageBox.Show("10% Battery Power Left!")
            End If
    
            If ProgressBar1.Value = 1 Then
                Timer2.Enabled = False
                Batch.Show()
                ProgressBar1.Value = 100
            End If
        End Sub

    Here is my current code for Batch:

    Public Class Batch
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
            If TextBox1.Text = "C:\ProgramFiles\FileSave\BatchOS\Command>quit" Then
                Me.Close()
                AppStore.Close()
                MainScreen.Close()
                Login.Close()
                Chat.Close()
                WindowsFiles.Close()
                Calc.Close()
                Reminders.Close()
                descofcapture.Close()
                AboutBox1.Close()
                AboutEclipse.Close()
                ScreenREC.Close()
                ScreenRECF2.Close()
                VirusScanner.Close()
                WebBrowserSCR.Close()
                Form1.Close()
                GmailSCR.Close()
            ElseIf TextBox1.Text = "C:\ProgramFiles\FileSave\BatchOS\Command>help" Then
                Label2.Text = "Help" + Environment.NewLine + Environment.NewLine + "quit: Leaves Program" + Environment.NewLine + "help: Shows Help" + Environment.NewLine + "recharge: Recharges the Program"
                TextBox1.Text = "C:\ProgramFiles\FileSave\BatchOS\Command>"
            ElseIf TextBox1.Text = "C:\ProgramFiles\FileSave\BatchOS\Command>recharge" Then
                TextBox1.Text = "C:\ProgramFiles\FileSave\BatchOS\Command>"
                Label2.Text = "Recharging..."
                Threading.Thread.Sleep(2000)
                Label2.Text = "Restarting will take 0.1% battery OFF your windows machine." + Environment.NewLine + "Type Confirm to confirm!"
            ElseIf TextBox1.Text = "C:\ProgramFiles\FileSave\BatchOS\Command>Confirm" Then
                TextBox1.Text = "C:\ProgramFiles\FileSave\BatchOS\Command>"
                Label2.Text = "Recharging program..."
                Threading.Thread.Sleep(2000)
                Label2.Text = "Recharged!"
                Threading.Thread.Sleep(1000)
                MainScreen.Timer2.Start()
                Me.Close()
            End If
        End Sub
    End Class

    Here is my code for a different timer with a 100 interval:

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Label10.Text = ProgressBar1.Value Label11.Text = Timer2.Interval End Sub

    Thanks in advance,

         Nave GCT

    P.S. Label12 shows the "." simbol.  Thanks

    Sunday, December 3, 2017 5:59 PM

Answers

  • Nave,

    You don't want to use a timer for this; instead, use a Stopwatch because what you're after is the .Elapsed property which presents you with a TimeSpan object.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by Nave GCT Sunday, December 3, 2017 6:08 PM
    • Unmarked as answer by Nave GCT Sunday, December 3, 2017 6:09 PM
    • Marked as answer by Nave GCT Sunday, December 3, 2017 6:09 PM
    Sunday, December 3, 2017 6:06 PM

All replies

  • Nave,

    You don't want to use a timer for this; instead, use a Stopwatch because what you're after is the .Elapsed property which presents you with a TimeSpan object.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by Nave GCT Sunday, December 3, 2017 6:08 PM
    • Unmarked as answer by Nave GCT Sunday, December 3, 2017 6:09 PM
    • Marked as answer by Nave GCT Sunday, December 3, 2017 6:09 PM
    Sunday, December 3, 2017 6:06 PM
  • How do i use a stopwatch?
    Sunday, December 3, 2017 6:08 PM
  • How do i use a stopwatch?

    You might set it up like the following. I have one label named "Label1" in Form1; the rest is in code:

    Public Class Form1
        Private _sw As Stopwatch
        Private WithEvents tmr As Timer
    
    
        Private Sub _
            Form1_Load(sender As Object, _
                       e As EventArgs) _
                       Handles MyBase.Load
    
            _sw = New Stopwatch
            _sw.Start()
    
            tmr = New Timer _
                With {.Interval = 1000, _
                      .Enabled = True}
    
            Label1.Text = ""
    
        End Sub
    
    
        Private Sub _
            tmr_Tick(sender As Object, _
                     e As System.EventArgs) _
                     Handles tmr.Tick
    
            If _sw IsNot Nothing AndAlso _sw.IsRunning Then
                Label1.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                                            _sw.Elapsed.Hours, _
                                            _sw.Elapsed.Minutes, _
                                            _sw.Elapsed.Seconds)
            End If
    
        End Sub
    End Class

    When you run that, you'll see the label display the elapsed time as "00:00:00". The timer is just there to update the label; it has nothing to with the stopwatch at all:


    "A problem well stated is a problem half solved.” - Charles F. Kettering




    Sunday, December 3, 2017 6:17 PM
  •  Instead of trying to guesstimate the battery life left,  or if it is at a low or critical state,  or even if the computer is plugged in or not (charging or not),  you can simply use the SystemInformation.PowerStatus Property to get all this information.  That property returns a PowerStatus Class.  All you would need to do is check the properties in a Timer Tick event and set the ProgressBar.Value or do whatever else like shutting down programs or saving data if the BatteryChargeStatus property is equal to BatteryChargeStatus.Critical and the PowerLineStatus property is PowerLineStatus.Offline indicating it is not plugged in.

    Test this in a new form project with 1 Label and 1 Timer added to the form.

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Interval = 1000
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim BatteryStatusInfo As New System.Text.StringBuilder
    
            With BatteryStatusInfo
                .AppendLine(SystemInformation.PowerStatus.PowerLineStatus.ToString)
                .AppendLine(SystemInformation.PowerStatus.BatteryChargeStatus.ToString)
                .AppendLine(SystemInformation.PowerStatus.BatteryFullLifetime.ToString)
                .AppendLine(SystemInformation.PowerStatus.BatteryLifePercent.ToString)
                .AppendLine(SystemInformation.PowerStatus.BatteryLifeRemaining.ToString)
            End With
    
            Label1.Text = BatteryStatusInfo.ToString
        End Sub
    End Class


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Monday, December 4, 2017 1:40 AM
    Monday, December 4, 2017 1:34 AM