Ubuntu Countdown Timer
Page 1 of 1 • Share
Ubuntu Countdown Timer
Quite some time went into this over the course of two weeks (since I had to figure everything out because Intro to Visual Basic doesn't deal with dates or timers or even saving simple settings). Here's how I did the Ubuntu Countdown Timer in Visual Studio 2k8.
Program can be downloaded here: box.net eiejhsl617
First off, the design. Since this was going to just be a timer and nothing else I decided on an equally simplistic design, Honestly though, you can go with whatever suits your fancy, the only thing you should keep in mind when designing is "Is this user friendly".
I like to use all labels since they're so easy to manipulate and that's what I did, I used all labels (just changed the preferences on how they looked). Also, to make the preferences window just double click on the word preferences (or whatever you choose to name it) and the program will start the code out for you as a click event.
The only thing I had to type in was "Preferences.Show()". And that's why Visual Basic is the easiest language to learn kiddies.
For the preferences window you're going to have to right click on the project name in the solution explorer and add a new .vb window form. (I named it preferences, because I'm such a good namer of things).
Note, subsequent code files will be hard to understand in their browser windows (unless you have a big monitor), best thing to do is add the code to notepad and turn wordwrap off.
Program can be downloaded here: box.net eiejhsl617
First off, the design. Since this was going to just be a timer and nothing else I decided on an equally simplistic design, Honestly though, you can go with whatever suits your fancy, the only thing you should keep in mind when designing is "Is this user friendly".
I like to use all labels since they're so easy to manipulate and that's what I did, I used all labels (just changed the preferences on how they looked). Also, to make the preferences window just double click on the word preferences (or whatever you choose to name it) and the program will start the code out for you as a click event.
- Code:
Private Sub uiPreferencesLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiPreferencesLabel.Click
preferences.Show()
End Sub
The only thing I had to type in was "Preferences.Show()". And that's why Visual Basic is the easiest language to learn kiddies.

For the preferences window you're going to have to right click on the project name in the solution explorer and add a new .vb window form. (I named it preferences, because I'm such a good namer of things).
Note, subsequent code files will be hard to understand in their browser windows (unless you have a big monitor), best thing to do is add the code to notepad and turn wordwrap off.
Last edited by The MacGuffin on Fri May 21, 2010 12:56 pm; edited 3 times in total
The MacGuffin- Ali Babba
- Number of posts : 201
Registration date : 2010-02-18
Re: Ubuntu Countdown Timer
Next up is the MainForm.vb code.
- Code:
Public Class MainForm
Private today As Date
Dim thisYear As Integer = Year(Date.Now)
Dim thisMonth As Integer = Month(Date.Now)
Dim daysInMonth As Integer = Date.DaysInMonth(thisYear, thisMonth)
Dim difference As Integer
Dim monthDifference, dayDifference, hourDifference, minuteDifference, secondDifference As Long
Dim timeSpan As TimeSpan
Dim todayPlusAMonth As Date = DateAdd(DateInterval.Month, 1, today) 'confirmed this works
Dim todayTotalDaysPlusAMonth As Long
Dim trueDayDifference As Long
Dim newPoint As New System.Drawing.Point
Dim X, Y As Integer
Private Sub MainForm_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
X = Control.MousePosition.X - Me.Location.X
Y = Control.MousePosition.Y - Me.Location.Y
End Sub
Private Sub MainForm_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
newPoint = Control.MousePosition
newPoint.X -= (X)
newPoint.Y -= (Y)
Me.Location = newPoint
My.Settings.WindowPosition = newPoint
End If
End Sub
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'had to put this in because the TopMost option didn't load unless you opened the preferences menu.
If My.Settings.TopMost = True Then
Me.TopMost = True
End If
'this is for the window position
Me.Location = My.Settings.WindowPosition
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.BackColor = Module1.backgroundColor
today = Date.Now
today = today
monthDifference = DateDiff(DateInterval.Month, today, releaseDate)
dayDifference = DateDiff(DateInterval.Day, today, releaseDate) 'this is the total number of days, not the true difference
todayTotalDaysPlusAMonth = DateDiff(DateInterval.Day, todayPlusAMonth, releaseDate)
trueDayDifference = dayDifference - todayTotalDaysPlusAMonth
trueDayDifference = trueDayDifference - today.Day - (trueDayDifference - releaseDate.Day)
TimeSpan = releaseDate.Subtract(today)
hourDifference = TimeSpan.Hours
minuteDifference = timeSpan.Minutes
secondDifference = timeSpan.Seconds
If secondDifference < 0 Then
secondDifference = secondDifference + 60
ElseIf secondDifference > 60 Then
secondDifference = secondDifference - 60
End If
If trueDayDifference < 0 Then
monthDifference = monthDifference - 1
trueDayDifference = trueDayDifference + daysInMonth
End If
difference = Date.Compare(today, releaseDate)
If difference < 0 Then
uiMonthsLabel.Text = monthDifference.ToString
uiDaysLabel.Text = trueDayDifference.ToString
uiHoursLabel.Text = hourDifference.ToString
uiMinutesLabel.Text = minuteDifference.ToString
uiSecondsLabel.Text = secondDifference.ToString
uiReleaseNameLabel.Text = releaseName
Else
uiReleaseNameLabel.Text = "TIME = OVER!"
End If
'don't mind me
Debug.WriteLine("Release Name: " & releaseName & vbNewLine & _
"Release Date: " & releaseDate & vbNewLine & _
"Release Date.Hour: " & releaseDate.Hour & vbNewLine & _
"Today: " & today.ToString & vbNewLine & _
"This Year: " & thisYear.ToString & vbNewLine & _
"This Month: " & thisMonth.ToString & vbNewLine & _
"Days In The Month: " & daysInMonth.ToString & vbNewLine & _
"Difference (Time End?): " & difference.ToString & vbNewLine & _
"Month Difference: " & monthDifference.ToString & vbNewLine & _
"Day Difference: " & dayDifference.ToString & vbNewLine & _
"Hour Difference: " & hourDifference.ToString & vbNewLine & _
"Minute Difference: " & minuteDifference.ToString & vbNewLine & _
"Second Difference: " & secondDifference.ToString & vbNewLine & _
"Today Plus a Month: " & todayPlusAMonth.ToString & vbNewLine & _
"True Day Difference: " & trueDayDifference.ToString & vbNewLine)
End Sub
Private Sub MainForm_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = Chr(27) Then '27 is the ASCII code for ESC.
Me.Close()
End If
End Sub
Private Sub uiPreferencesLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiPreferencesLabel.Click
preferences.Show()
End Sub
End Class
The MacGuffin- Ali Babba
- Number of posts : 201
Registration date : 2010-02-18
Re: Ubuntu Countdown Timer
Since the code tags only print out in green it'll be a little difficult to sort through. As a rule anything with ' in front of it is a comment, or something that is ignored by the compiler.
I chose to have all my variables class level out of habit, you don't need to do it though I find it easier if I ever need to use a variable in two parts of the code. (since variables written inside a private sub stay in a private sub).
The two _MouseMove events are coupled together, the first one gets where the mouse position is and applies it to the variables X and Y. Then the next one uses those variables to determine where you moved the program and saves it to the settings (more on settings later.
In visual basic if you want to know what a section of code does look at the _***** section of the code right after the private sub, easy. I'm going to skip the mainform_Load for now since I said more on settings later.
Timer1_Tick. This is just adding a simple timer to the program. You can activate the _tick event by double clicking (normally this would activate the _Click event but since the Timer is not seen by the user the default is _Tick (or every time the tick event is scheduled to happen (see preferences)). This is where all the math comes in (oh silly, math is everywhere, you will never escape it). This was originally difficult because my teacher, when asked, told me to use the DateDiff function. Which is great when you want the total number of hours between two dates, not so great if you don't want to see, "4 months, 126 days, 24123 hours.....". So that was only useful for figuring out the days and months. What I had to do was add a month onto the current date and subtract the total days from that date. Look at the msdn of the micro$oft website if you want a more in-depth look at the DateDiff and other functions (seriously, go do that, use Google if you can't find it). The reason there is pieces of code that makes sure seconds doesn't go lower than 0 and no more than 60 was because if you have a dateTime that is less than the current time then you will get a negative seconds or seconds above 60. Same applies for the Day difference.
The difference variable holds the date.compare function (spits out a -1 or 0 or 1 depending on the location of the date it's supposed to be counting down to and today. So if today is less than the countdown date difference = -1. We can use this to determine if the date has arrived.
All the next part does is add the differences to the labels (which are invisible to the user if nothing is added), this section should be obvious.
Debug.WriteLine() is a function that spits out what the variables equal in a different window (since if you never see the lesser parts of the math code you'll want to make sure they're spitting out the correct number. I highly recommend you always do a debug.WriteLine for all code.
Ignore the _KeyPress event, all this does is check if the ESC is pressed, and if it is, closes the form. You'll learn this in any Intro class.
I chose to have all my variables class level out of habit, you don't need to do it though I find it easier if I ever need to use a variable in two parts of the code. (since variables written inside a private sub stay in a private sub).
The two _MouseMove events are coupled together, the first one gets where the mouse position is and applies it to the variables X and Y. Then the next one uses those variables to determine where you moved the program and saves it to the settings (more on settings later.
In visual basic if you want to know what a section of code does look at the _***** section of the code right after the private sub, easy. I'm going to skip the mainform_Load for now since I said more on settings later.
Timer1_Tick. This is just adding a simple timer to the program. You can activate the _tick event by double clicking (normally this would activate the _Click event but since the Timer is not seen by the user the default is _Tick (or every time the tick event is scheduled to happen (see preferences)). This is where all the math comes in (oh silly, math is everywhere, you will never escape it). This was originally difficult because my teacher, when asked, told me to use the DateDiff function. Which is great when you want the total number of hours between two dates, not so great if you don't want to see, "4 months, 126 days, 24123 hours.....". So that was only useful for figuring out the days and months. What I had to do was add a month onto the current date and subtract the total days from that date. Look at the msdn of the micro$oft website if you want a more in-depth look at the DateDiff and other functions (seriously, go do that, use Google if you can't find it). The reason there is pieces of code that makes sure seconds doesn't go lower than 0 and no more than 60 was because if you have a dateTime that is less than the current time then you will get a negative seconds or seconds above 60. Same applies for the Day difference.
The difference variable holds the date.compare function (spits out a -1 or 0 or 1 depending on the location of the date it's supposed to be counting down to and today. So if today is less than the countdown date difference = -1. We can use this to determine if the date has arrived.
All the next part does is add the differences to the labels (which are invisible to the user if nothing is added), this section should be obvious.
Debug.WriteLine() is a function that spits out what the variables equal in a different window (since if you never see the lesser parts of the math code you'll want to make sure they're spitting out the correct number. I highly recommend you always do a debug.WriteLine for all code.
Ignore the _KeyPress event, all this does is check if the ESC is pressed, and if it is, closes the form. You'll learn this in any Intro class.
The MacGuffin- Ali Babba
- Number of posts : 201
Registration date : 2010-02-18
Re: Ubuntu Countdown Timer
Also, make sure all preferences are double checked. By default the Timer1 is disabled (to be enabled by code or the user) but since mine is a countdown timer it should always be enabled. This can cause a lot of confusion if you don't have this checked by default or have something in your code that does it for you.
The MacGuffin- Ali Babba
- Number of posts : 201
Registration date : 2010-02-18
Re: Ubuntu Countdown Timer
The Preferences.vb window.
Contents: A check box, another label which opens up a color picker (I'll tell you how in the code), a name label followed by a text box, and a date time picker with a few buttons thrown in. Also a Color Picker and another Timer.
Now for the code:
Contents: A check box, another label which opens up a color picker (I'll tell you how in the code), a name label followed by a text box, and a date time picker with a few buttons thrown in. Also a Color Picker and another Timer.
Now for the code:
- Code:
Option Explicit On
Option Strict On
Option Infer Off
Public Class preferences
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
Dim newColor As Color
ColorDialog1.ShowDialog()
newColor = ColorDialog1.Color
Module1.backgroundColor = newColor
My.Settings.Color = newColor
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call Reset()
Me.Close()
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.CheckState = CheckState.Checked Then
MainForm.TopMost = True
My.Settings.TopMost = True
ElseIf CheckBox1.CheckState = CheckState.Unchecked Then
MainForm.TopMost = False
My.Settings.TopMost = False
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.BackColor = Module1.backgroundColor
Button1.BackColor = Module1.backgroundColor
End Sub
Private Sub preferences_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If My.Settings.TopMost = True Then
CheckBox1.CheckState = CheckState.Checked
Else
CheckBox1.CheckState = CheckState.Unchecked
End If
'DateTimePicker1.Value = My.Settings.releaseDate
'DateTimePicker1.Value = CDate(Format(DateTimePicker1.Value, "mm/dd/yyyy"))
'causes annoying error and is completely useless, there isn't a way to change the default date time.
End Sub
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
Dim newDate As Date
newDate = DateTimePicker1.Value
My.Settings.releaseDate = newDate
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim newName As String = String.Empty
newName = TextBox1.Text
My.Settings.releaseName = newName
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
Last edited by The MacGuffin on Fri May 21, 2010 12:43 pm; edited 1 time in total
The MacGuffin- Ali Babba
- Number of posts : 201
Registration date : 2010-02-18
Re: Ubuntu Countdown Timer
Options are there as an application scope option to make sure you don't make any mistakes like converting a variable to something else weird.
I got lazy in labeling my labels (always do when it's a preference window and it's a bad habit). Anyway, label1 is the color changer. In a nutshell this is what that section does, creates a new color per the users choice and stores it in newColor. Then it changes the background option (it's a module variable. And if you noticed that there was a variable in the MainForm.vb code that wasn't created, CONGRATULATIONS! You get a cookie and a star to put by your name. I'll tell you how to use module variables in a bit). Then it adds that variable to the My.Settings.Color option which I should explain to you now since that's what I used to make sure the program can remember what you put in even if you shut down your computer.
In the top menu bar click Project, then click *Project Name* Properties option (usually it's the last one). Then click Settings. Here is where the magic happens. You have no options probably but we're going to add some based on the variables used in the code. To add a variable simply start typing it's name. Here is what I used.
Name:
TopMost
Type:
Boolean
Scope: (The only importance in this option is weather you want your user to be able to change this option or not)
User (to let the user change the variable)
Value:
False
And you've added your first setting that will save itself even if you restart your computer. And no messing around with dinky .txt files! The other options are Color (as system.drawing.color) WindowPosition (as system.drawing.point) releaseName and releaseDate (as string and date respectively). You will want to add just the date and ignore the time, it adds too many complications that I don't like to deal with
.
Now we need some module level variables that will allow all forms to be able to change them.
Right click your project name in the Solution Explorer and add a module.vb form. This is what I added.
Public for everyone to alter, variable name, variable type, and what it equals. This awesome module form will load once at startup for anything that is a public declaration. In the second part, the Public Sub Reset(), the Sub part of this declaration means that it isn't loaded when the program loads, and can only be loaded if it is called. (look again at the preferences.vb code, You'll find the code "Call Reset" under the okay button. This makes it so that the variables you changed stay changed, unlike the Cancel click event which just closes the form. (again, sorry I got lazy, button1 is the okay button while button2 is cancel).
You will also notice that I have another timer for the preferences.vb form. (yes there are two timers, not one.) This makes sure the background color stays the same. And the rest is self explanatory.
I got lazy in labeling my labels (always do when it's a preference window and it's a bad habit). Anyway, label1 is the color changer. In a nutshell this is what that section does, creates a new color per the users choice and stores it in newColor. Then it changes the background option (it's a module variable. And if you noticed that there was a variable in the MainForm.vb code that wasn't created, CONGRATULATIONS! You get a cookie and a star to put by your name. I'll tell you how to use module variables in a bit). Then it adds that variable to the My.Settings.Color option which I should explain to you now since that's what I used to make sure the program can remember what you put in even if you shut down your computer.
In the top menu bar click Project, then click *Project Name* Properties option (usually it's the last one). Then click Settings. Here is where the magic happens. You have no options probably but we're going to add some based on the variables used in the code. To add a variable simply start typing it's name. Here is what I used.
Name:
TopMost
Type:
Boolean
Scope: (The only importance in this option is weather you want your user to be able to change this option or not)
User (to let the user change the variable)
Value:
False
And you've added your first setting that will save itself even if you restart your computer. And no messing around with dinky .txt files! The other options are Color (as system.drawing.color) WindowPosition (as system.drawing.point) releaseName and releaseDate (as string and date respectively). You will want to add just the date and ignore the time, it adds too many complications that I don't like to deal with

Now we need some module level variables that will allow all forms to be able to change them.
Right click your project name in the Solution Explorer and add a module.vb form. This is what I added.
- Code:
Module Module1
Public backgroundColor As Color = My.Settings.Color
Public topMost As Boolean = My.Settings.TopMost
Public releaseName As String = My.Settings.releaseName
Public releaseDate As Date = My.Settings.releaseDate
Public Sub Reset()
releaseDate = My.Settings.releaseDate
releaseName = My.Settings.releaseName
End Sub
End Module
Public for everyone to alter, variable name, variable type, and what it equals. This awesome module form will load once at startup for anything that is a public declaration. In the second part, the Public Sub Reset(), the Sub part of this declaration means that it isn't loaded when the program loads, and can only be loaded if it is called. (look again at the preferences.vb code, You'll find the code "Call Reset" under the okay button. This makes it so that the variables you changed stay changed, unlike the Cancel click event which just closes the form. (again, sorry I got lazy, button1 is the okay button while button2 is cancel).
You will also notice that I have another timer for the preferences.vb form. (yes there are two timers, not one.) This makes sure the background color stays the same. And the rest is self explanatory.
Last edited by The MacGuffin on Fri May 21, 2010 12:47 pm; edited 2 times in total
The MacGuffin- Ali Babba
- Number of posts : 201
Registration date : 2010-02-18
Re: Ubuntu Countdown Timer
You may now ask me stuff.
The MacGuffin- Ali Babba
- Number of posts : 201
Registration date : 2010-02-18

» Binary Countdown
» Raptor Chess Interface
» Infinity Chess
» About Ubuntu
» How to compile Stockfish chess engine
» Raptor Chess Interface
» Infinity Chess
» About Ubuntu
» How to compile Stockfish chess engine
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|