Introduction
Initially, the article idea I had was for a simple countdown application. Honestly, there is only so much I can write about in a countdown article, so I decided to give the article more meat. Today, I will show you not only how to make a countdown application, but use a timer control to display the exact time your application has been running.
Timers
Refer to these articles for more information regarding Timers:
- https://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx
- https://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.110%29.aspx
- https://msdn.microsoft.com/en-us/magazine/cc164015.aspx
Our Project
The application that you will build today will achieve two things:
- Count down from a certain point in time.
- Calculate exactly for how much time your application has been running.
Design
Create a new Visual Basic Windows Forms application and design it shown in Figure 1:
Figure 1: Our design
Code
Counting Down Time
Create the following modular variables:
Private timeEnd As DateTime Private timeDiff As TimeSpan
These two variables will be responsible for calculating the time left on our little “alarm clock.”
Add the following code inside the Form_Load event:
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load txtMinute.Text = 10 txtHour.Text = 1 txtSecond.Text = 15 End Sub
Here, although it should be glaringly obvious, I set the starting values for the hours, minutes, and seconds. The countdown will start from whatever values the user enters inside these respective textboxes.
Add the following code to the button labeled “Down“:
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click timeEnd = DateTime.Now Dim hour As Double = System.Convert.ToDouble(txtHour.Text) Dim minute As Double = System.Convert.ToDouble(txtMinute.Text) Dim second As Double = System.Convert.ToDouble(txtSecond.Text) timeEnd = timeEnd.AddHours(hour) timeEnd = timeEnd.AddMinutes(minute) timeEnd = timeEnd.AddSeconds(second) Timer1.Start() End Sub
Inside the button’s click event, I set the ending point for the countdown timer. By the looks of it, it initially probably does not make sense at first. Why? Well, the first line of code simply initializes the timeEnd variable, not the actual ending time!
The few variables that get created when this button has been clicked will control and set the values to the timeEnd variable. Whatever has been entered inside the respective textboxes gets converted to a double object, then via the timeEnd’s AddHours, AddMinutes, and AddSeconds methods I set the exact ending time.
To make this work, add the Timer’s Tick event:
Private Sub Timer1_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Timer1.Tick timeDiff = timeEnd - DateTime.Now Dim output As TimeSpan = New TimeSpan(timeDiff.Hours, _ timeDiff.Minutes, timeDiff.Seconds) txtRemaining.Text = output.ToString If (timeDiff.Ticks < 0) Then Timer1.Stop() MessageBox.Show("ZERO!!!!") End If End Sub
timeDiff calculates the exact ending time by subtracting the current time value with the TimeEnd object we created earlier. Output formats the current time value and displays it inside the txtRemaining textbox. When time has run out, a MessageBox pops up, indicating that the time has expired.
If you were to run the application now, you would see it counting down from whatever time value you have supplied, as shown in Figure 2:
Figure 2: Countdown
Counting Time Upwards
Add the next modular variable:
Private CompTime As System.Int32
Add the next code segment:
Private Sub Button2_Click(sender As Object, _ e As EventArgs) Handles Button2.Click CompTime = Environment.TickCount Timer2.Start() End Sub
Comptime calculates the exact time via the use of Environment.TickCount. Now, add the final piece of the puzzle:
Private Sub Timer2_Tick(sender As Object, _ e As EventArgs) Handles Timer2.Tick Dim CurTickValue As System.Int32 = Environment.TickCount Dim Difference As System.Int32 = CurTickValue - CompTime Dim Hours As System.Int32 Dim Minutes As System.Int32 Dim Seconds As System.Int32 Hours = (Difference / (3600 * 999)) Mod 24 Minutes = (Difference / (60 * 999)) Mod 60 Seconds = (Difference / 999) Mod 60 Label1.Text = String.Format("This application has been _ running for {0} hours, {1} minutes {2} seconds", _ CStr(Hours), _ CStr(Minutes), _ CStr(Seconds)) End Sub
Again, I calculate the time difference, except for now it works forward and not backwards. I created a few objects, and then here is the fun part! How good is your math? No, just kidding; it doesn’t have to be so good to be able to understand the underlying logic here.
This line:
Hours = (Difference / (3600 * 999)) Mod 24
calculates the hourly difference. I took 3600 * 999 then divided by the time difference and then used modulo 24. Now what does that mean?!
Let me break it down:
- 3600 * 999: 60 Seconds multiplied by 60 minutes (ultimately, one hour) is 3600. Multiply this value with 999 (to get a difference) gives us a value to work with.
- Difference / above answer: Gives us an answer in hours.
- The above answer Mod 24: There are 24 hours in a day, and we need to find a value that is divisible by 24 to display the exact hours.
Minutes = (Difference / (60 * 999)) Mod 60
- It basically does the same computation, except to multiply only 60—for minutes—and does a remainder division with 60, again minutes.
Seconds = (Difference / 999) Mod 60
- Computes the seconds.
The last line of code simply formats the computed values into a displayable string. When run, your application will resemble Figure 3:
Figure 3: Count upwards
Conclusion
I have included a working sample with this project. I hope you have benefited from this article.