TonysOpenSource

Code Snippets and Essays from the spare time

  

StopWatch

This project presents a stopwatch application that was developed due to my taking a mathematics university course and because it's been a long time since I did any maths academically I thought I'd better start getting some practice in. This meant taking timed tests and not having a decent way of timing the tests I thought I'd better write one, and due to being a programmer a simple time telling device was not enough, it had to look reasonable and get my attention when I was concentrating on something else.

The application was developed under Windows XP Home edition and tested on Windows 2k. It is comprised of three main parts, these being, The main dialog that controls the program, a user control that accepts the time entries and a user control that displays the clock face. There is also a Microsoft MediaPlayer object added to the control.

 

The Main Form

The purpose of the main form is control the feedback of the program and allow the user to run it in anyway that they choose. when the program is run and a time value is entered the points for the form to take into account are is the form to displayed which is set by checking the Show Clock check box and which way is the count to go which is controlled by the Count Down check box which is set to checked by default. At this point the Play Sound File check box can be selected to indicate that a chosen file is to be played once the count down or count up is finished.

If the user chooses not to display the clock while the timer is running, possibly because they are taking a timed test on the computer then the program can be controlled via the Icon that will appear on the taskbar when the application is running. A taskbar icon is set up by selecting a Notifyicon object from the toolbar and adding it to the form. All that is then required is that an icon that is to be displayed is added through the properties part of the user interface. A context menu will also need to be added and this is done by again choosing a context menu object from the toolbar and dropping it onto the form. The menu will then need to be edited so that the items that are required appear when there is a mouse click on the icon that appears on the taskbar. Getting the context menu and the required functions can be a bit tricky to get exactly right at first as to the absolute begginer it can be a little unclear when you are editing the menu option or the context menu id. There's no scientific solution to this it just takes a bit of practise.

There are two other parts to the main form the that have minor roles the first is the seperate timer that the form has this is purely for house keeping and is used to check if the DigitalClock control has finished and the OpenFileDialog which is used for selecting the sound file to be played.

Customizing The Toolbar

This is an important part of this application so it should be understood right from the start. The default setup for Developer Studio .NET does not include every single thing in the toolbar that could be included. This means that occaisionally you need to add things for yourself, this is done by selecting the toolbar and then right clicking on it so that a context menu pops up. At half way down the menu is the Customise Toolbox option which will open a dialog that allows you to add some of the controls that have not been selected so far. These can be com objects or Custom Controls that have been created in CSharp or any other .NET language. Once this project has been compiled on a computer the DigitalClock control can be added to the toolbar in this manner.

The TimeSet Control

While not offically being part of this project the TimeSet Dll is used within it and the project file for it is included in the zip file. This is a very simple control for entering time values in hours minutes and seconds if required and uses standard up down value controls that are set to go no higher than the maximum value allowed for each item. eg the minutes box will not accept a value greater than sixty if the up down buttons are used.

The DigitalClock Control 

The DigitalClock control is the main display piece of the program and uses the SevenSegmentDisplay class from Charles Peltzold's book Programming Microsoft Windows with C#. This class is used as is with no editing by me and so if an explanation of the code is required I suggest you buy the book. If an explanation isn't required you should still by the book as if you weren't interested in CSharp you wouldn't be reading this.

There is a fair bit of house keeping code in the DigitalClock.DigitalDisplay class most of which is simply keeping track of what the hours minutes and seconds are and then drawing the display. 

Adding The Media Player

To add the Media Player to an application add it to the toolbar as described above and the drop it onto the form. This will add two dll files to the executable directory for your project. In this case they are the  Interop.MediaPlayer.dll which contains the code for talking to the com component of the media player and the AxInterop.MediaPlayer.dll which contains the code that the application talks to directly that is then fed through the Interop.MediaPlayer.dll to the Media Player itself.

Despite this the MediaPlayer itself is very easy to use. Once you have dropped it on the form the form class declares a variable for you within the class. All the code has to do then in this case is set the filename that is to be played and then call stop because the Media Player will start playing the file straight away if this isn't done.

One problem with the Media Player is that the code initially sets the volume to -600 this seemed to be rather quiet and putting the value at 0 seems to play the file at the current volume setting on the computer. However if a positive value is entered the Media Player would throw an exception, so the code sets the vlume to zero. The code also only allows the opening of mp3 files to be played. This is because that is what I wanted to open if anyone requires other types of files to be played they will need to change the parameters used for the OpenFileDialog.

Download the source code