Jump to content
xisto Community
rvalkass

Creating A Timer Program Using Visual Basic 2005

Recommended Posts

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. Screenshot

 

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:

Public Class Form1End 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 (Screenshot). 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:

		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:

		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.

Share this post


Link to post
Share on other sites

I've tryed this one and I think it's not working. Somehow the timer doesn't start and I don't get the reminder to fire up.I've even replaced Timer1.Interval = Minutes * 60 * 1000 with Timer1.Interval = 5000 ( 5 sec) and doesn't start either.Can you please tell me why ?

Share this post


Link to post
Share on other sites

vb program

Creating A Timer Program

 

Can you help me with this project? This is the problem: Consider a computer system in which "computer games" can be played by students only between 10pm and 6am, by faculty members between 5 pm and 8am, and by the computer staff at all times. Make a simple program using vb you want to answer the scenario above.

 

-reply by Ruben Vasquez

Share this post


Link to post
Share on other sites

Nice, this looks like a really good and easy tutorial for beginners to visual basic. Also, I didnt know there was a free version, so thanks for that also :lol:

Share this post


Link to post
Share on other sites

Or, alternatively, if you want to use the timer to keep track of some event, instead of a reminder, you can set the following as well:

Dim seconds as Integer = NumericUpDown1.Value * 60MsgBox("The timer was set to " & seconds \ 60 & " minutes and " & seconds Mod 60 " seconds.")

The backslash is integer division. Basically, 3 \ 2 = 1.5, which rounds down to 1.

To make this work, you will have to preset the Timer's interval to 1000, as well as add a label. Name it Label1.

In the code display, under Timer1.Tick, add the following code:

seconds = seconds - 1Label1.Text = seconds \ 60 & ":" & (seconds \ 6) mod 10 & seconds Mod 60

This little tidbit of code displays the minutes:seconds. For example, if the time left was 8 minutes and 30 seconds, It would display "8:30".

Then add:

If seconds = 0 thenMsgBox(TextBox1.Text)

like the original program.

Share this post


Link to post
Share on other sites
Timer wonCreating A Timer Program

I hope this question isn't too lame, but I'm trying to write a VB program that will allow me to time how quickly a (supposedly in MS Studio 10) 64 bit variable can count up on my 64 bit laptop? I was hoping to have an infinite 'while' loop with, for instance, a global double ctr1 = ctr1 + 1 that would begin execution upon the push of a button, and the value of ctr1 would be displayed once every second in a text box with the timer1. Problem seems to be that the timer will not interrupt this 'while' loop! (Heck, I can't even get the timer to display the value AFTER it has finished counting in a for-next loop!) What gives?

-question by anon

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.