Navigation


Creating A Timer Program, Using Visual Basic 2005

June 11th, 2007, Visual Basic, General, admin, 0 Comments Print This Post Print This Post

This tutorial will explain how to create a basic timer using Visual Basic Express 2005. If you don’t have it, it’s free and you can dowload it from Microsoft’s website. All you need is a few minutes to sit down and read this and a version of Visual Basic. OK, so what will this timer actually do? Well, you are able to enter a number of minutes and a message, and then click a button. Once the timer is up, your message pops up and you are reminded! So, basically it’s a little reminder system. I use it to remind me when TV programmes start, when I have to go somewhere, all sorts of things, and I’m sure you’ll be able to find a use for it too.

Start off by opening Visual Basic. I’m using the 2005 Express Version, but this should work with most versions, but things will look different. Create a new project by clicking File > New Project…. Give it a logical name and click on OK.

You are presented with the customary blank form. Drag on a NumericUpDown, a TextBox and a Button. Arrange them something like this, but you can use your own layout. Then drag on a Timer, and you can place this anywhere, as it will appear at the bottom of the screen. That’s the layout done!

OK, onto the code. Right click on a blank section of the form, and select View Code. You will now be presented with this:
CODE

Public Class Form1

End Class

What we need to do first is detect when the button is clicked, and start the timer up. Above the code area there are two drop down boxes. These allow you to choose various actions for the code. Change the left hand side one to say Button1 and the right hand one to Click . Some code will be automatically added, and whatever you put between it will happen when the button is clicked. Between those two lines, add the following code:
CODE

Dim Minutes As Integer = NumericUpDown1.Value
Timer1.Interval = Minutes * 60 * 1000
Timer1.Enabled = True
MsgBox(”The timer has been set.”, MsgBoxStyle.Information And MsgBoxStyle.OkOnly, “Timer Set”)

Lets go line by line. The first line creates a new variable which will store our time. It takes the number from our NumericUpDown1 and stores it as an integer. The next line sets the interval of the clock to 60,000 times what is in the variable. This is becuase the timer only accepts times in milliseconds, so if we times the number of minutes by 60,000 we convert it to milliseconds. The third line starts the timer, and the last line prompts you with a notice to say it works.

Now we need to set what happens when the timer has finished. Back on those two drop down lists, choose Timer1 in the first one and Tick in the second. Between the two new lines, type this:
CODE

Dim Reminder As String = TextBox1.Text
MsgBox(Reminder, MsgBoxStyle.Information And MsgBoxStyle.OkOnly, “Your Reminder!”)
Timer1.Enabled = False

Again, line by line. The first line takes the text you type in the text box and copies it to a variable. This variable is then set as the main text in a message box, which is displayed when the timer is up (Screenshot). The final line turns the timer off, otherwise it would run infinitely and you would be reminded every few minutes of your message.

That’s it, the code is finished. To try it, click on the green start arrow in the toolbar at the top, or push F5. You can of course add labels to the form to give some idea of what needs to be written in it. One modification I would definitely make, however, is making the text box accept multiple lines, which can be displayed in your message box. To do this, go back to the form designer tab, and click on the text box. On the right hand side, in the properties panel, scroll until you find the property Multiline and change it to True. Now you can drag the text box to make it taller and fill the available space on the form. You can also rearrange things and change the size of the form.

After a bit of touching up, mine looks like this. To turn your application into an EXE file you can run, click on Build > Build Project Name. If you get any errors, post them here, PM me or email me and I’ll do my best to help.

Webhosting - 0.99$/Pay monthly 1 GB Space, 5GB BW, Cpanel etc

del.icio.us Digg it Earthlink Furl iFeedReaders ma.gnolia Maple.nu Netvouz Netscape RawSugar reddit Scuttle Shadows Simpy Spurl StumbleUpon Wink Yahoo MyWeb


Last 25 post


Subcribe

    Add to Google Reader or Homepage

    Subscribe in NewsGator Online

    Add to My AOL

    Add Ask N Answer to Newsburst from CNET News.com

    Subscribe in Bloglines

    Subscribe in FeedLounge

Pages


Category


Archive


Links