We’re about to move and I’ll be out of pocket for several days as we cart all our stuff to the new house. In the meantime, my six year old keeps asking “how much longer until we move, daddy?”. I finally decided that I would make a simple little timer to not only show how long until we start moving, but several other key events of the move as well because I find myself counting down the days as well (yeah, I really want out of this house). So I did and I’m going to put the code here. There’s nothing earth shattering here – it’s my typical “Stupid Coding Tricks” blog post. The whole point of these sorts of things is to show how a simple little program that can be written in a few minutes can still be useful. I decided to build this little app in winforms because it was the simplest thing that could possibly work.
First, I considered what I wanted to track. I came up with four things:
- The time remaining until we signed the papers selling our current house (Freedom, to be sold on 9/25/2014 at 9:30 AM)
- The time remaining until we signed the papers purchasing our new house (Wabash, to be signed on 9/25/2014 at noon, an hour’s drive away from where we signed the freedom papers)
- The time remaining until the professional movers showed up (9/26/2014 9:30 AM)
- The time remaining until we contractually have to be out of the old house (we’re getting out well beforehand)
With these things in mind, I build the following form in winforms. The controls shown below are all labels.
With that done, all I needed to do was calculate the time remaining to each of the above events.
1 | public partial class Form1 : Form |
At the top, I’m simply declaring DateTime constants for the various events. I’m also declaring a Timer object that will be used to update the display every second. I’m also declaring the time format string once, because I’m lazy and don’t wish to type repetitively.
In the form’s constructor, after the InitializeComponent call, I’m setting the interval of the timer and then giving it a callback to call when it fires (Timer_Tick) before setting it enabled.
When the timer fires, I get the current time into the “now” variable and then quickly calculate the difference in times for each of the events. Then I set the labels on the form, using the format string I set up above. When it runs, the UI looks like the image below (but it obviously updates once per second).
Obviously this is pretty much the simplest program I’ve ever shown on this site. But that’s really the point of the whole thing. Very small, uncomplicated programs can be extremely helpful at times. This is why I think it behooves nearly everyone to learn basic programming skills – you can build small, useful projects that provide value for you without having to be perfectly architected by someone charging three figures an hour.