How to make a Stopwatch

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
6 posts Page 1 of 1
Contributors
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

How to make a Stopwatch
Usman55
Hello everyone,

Today I'm going to show you how to create a stopwatch application. I just made one yesterday and it was a basic one, so I'm posting a tutorial. What this stopwatch does it is that you can start it, stop it, reset it, and mark the current time. Please don't post negative/disappointing comments.

---------------------------------------------------------------------------

First of all, open Visual Basic 2008, create a new project, choose Windows Forms Application, type in the textbox Stopwatch and press the OK button. As soon as you have done this, the Form1 will appear on the screen on which we will design our application and code it.


First we will do the designing and then the coding. So now we will change the Form1's properties to make it look good. Change the Form1's Text property to Stopwatch. Change it's StartPosition property to CenterScreen. Change it's FormBorderStyle to FixedSingle. Change it's MaximizeBox property to False. Change it's Size property to 537, 174.


Now add the following things to the form:

(1)_ 1 Label. Change it's AutoSize property to False and it's BorderStyle property to FixedSingle. Resize it to fit the top of the form. Change it's TextAllign property to MiddleCenter. Change it's Font property to "Microsoft Sans Serif, 39.75pt, style=Bold" and it's Text property to "00:00:00:000".

(2)_ 4 Buttons. Change Button1's Text property to Start, Button2's to Stop, Button3's to Reset and Button4's to Mark.

(3)_ 1 ListBox.

(4)_ 1 Timer. Change it's Interval property to 1.


Now is the time to code Form1 and it's components. So right-click on the form in the Solution Explorer and click View Code.

Type the following code before Public Class Form1:
Code: Select all
Dim StopWatch As New Diagnostics.Stopwatch
Write the following code by double-clicking Timer1:
Code: Select all
Dim elapsed As TimeSpan = Me.StopWatch.Elapsed
        Label1.Text = String.Format("{0:00}:{1:00}:{2:00}:{3:00}", Math.Floor(elapsed.TotalHours), elapsed.Minutes, elapsed.Seconds, elapsed.Milliseconds)
This is the code for Button1 which we call Start:
Code: Select all
Timer1.Start()
        Me.StopWatch.Start()
This is the code for Button2 which we call Stop:
Code: Select all
Timer1.Stop()
        Me.StopWatch.Stop(
This is the code for Button3 which we call Reset:
Code: Select all
Me.StopWatch.Reset()
        Label1.Text = "00:00:00:000"
        ListBox1.Items.Clear()
This is the code for Button4 which we call Mark:
Code: Select all
ListBox1.Items.Add(Label1.Text)

Well, that's all. It was real easy, wasn't it?

---------------------------------------------------------------------------

Voila! You have just completed making a Stopwatch with some easy-to-use features. Now debug and run it by pressing the Start Debugging button located at the main toolstrip. Test it, if it works fine then its SAVING time. You can save your project by going to File -> Save All and then pressing Save.

And there! You have your project and application that works. If you have any problem or can't understand something, please feel free to ask by either a comment or by PM. I have also attached a screenshot.

Please use the Give Thanks button and the Reputation System to appreciate my hard work. I have wrote this tutorial while making a Stopwatch project myself so you can download the source file in the attachments.

Thank you.

Image
Image
User avatar
CleverBoy
VIP - Donator
VIP - Donator
Posts: 395
Joined: Mon Dec 06, 2010 8:29 pm

Re: How to make a Stopwatch
CleverBoy
Great tutorial yours is better than mine 100%
+rep
Code'N'Stuff
OneTeam..OneDream
Join ABSplash Team & Earn $$
ABSplash Site - Workpad - (VB) Custom Buttons 2 ways
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: How to make a Stopwatch
Usman55
Thanks dude, I really appreciate it.
Image
User avatar
Rookie
Top Poster
Top Poster
Posts: 85
Joined: Thu May 19, 2011 10:28 am

Re: How to make a Stopwatch
Rookie
Great fight; fight;
User avatar
darkseven123
New Member
New Member
Posts: 10
Joined: Fri Oct 14, 2011 1:24 am

Re: How to make a Stopwatch
darkseven123
Really nice.
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

Re: How to make a Stopwatch
clanc789
Good tutorial, simple yet effective!
Practice makes perfect!

VIP since: 6-10-2011
6 posts Page 1 of 1
Return to “Tutorials”