IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet

Creating A Timer Program

, Using Visual Basic 2005


rvalkass
no avatar
apt-get moo
****************
Group: [MODERATOR]
Posts: 2,782
Joined: 28-May 05
From: Devon, England
Member No.: 7,593
Spam Patrol
myCENT:61.50



Post #1 post Apr 6 2006, 08:43 AM
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:
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 (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:
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.
Go to the top of the page
+Quote Post
 
Start new topic
Replies (1 - 8)
kody
no avatar
Newbie
*
Group: Members
Posts: 1
Joined: 27-October 06
Member No.: 32,332



Post #2 post Oct 27 2006, 05:59 PM
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 ?
Go to the top of the page
+Quote Post
timvk
no avatar
Newbie
*
Group: Members
Posts: 2
Joined: 25-September 07
Member No.: 50,629



Post #3 post Sep 25 2007, 07:17 PM
i made another topic for my question:

http://www.trap17.com/forums/Timer-Control-Key-Press-Minute-Vb2005-t51758.html


hope you could help me, sorry
Go to the top of the page
+Quote Post
de4thpr00f
no avatar
Member [Level 2]
*****
Group: Members
Posts: 76
Joined: 21-November 07
Member No.: 53,412



Post #4 post Nov 22 2007, 05:29 AM
This is very useful, it's working. I just set this to run on background so i'm not bothered with the minimized window biggrin.gif

great job.
Go to the top of the page
+Quote Post
Forbez
no avatar
Privileged Member
*********
Group: [HOSTED]
Posts: 662
Joined: 13-December 06
Member No.: 35,271
myCENT:49.54



Post #5 post Nov 22 2007, 04:26 PM
I'll try this later, but so far it's looking really good. Nicely written and easy to follow.

Thanks.
Go to the top of the page
+Quote Post
iGuest
no avatar
Hail Caesar!
*********************
Group: Members
Posts: 5,876
Joined: 21-September 07
Member No.: 50,369



Post #6 post Mar 17 2008, 10:16 AM
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
Go to the top of the page
+Quote Post
azri92
no avatar
Newbie
*
Group: Members
Posts: 6
Joined: 9-February 08
Member No.: 57,617



Post #7 post Mar 20 2008, 09:29 AM
good job
Go to the top of the page
+Quote Post
FLaKes
no avatar
Trap Grand Marshal Member
***********
Group: [HOSTED]
Posts: 1,142
Joined: 19-May 05
From: Mexico
Member No.: 7,234
myCENT:NEGATIVE[-36.23]



Post #8 post Mar 20 2008, 06:34 PM
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 smile.gif
Go to the top of the page
+Quote Post
tricky77puzzle
no avatar
Super Member
*********
Group: [HOSTED]
Posts: 416
Joined: 26-January 08
Member No.: 56,881



Post #9 post Mar 20 2008, 11:43 PM
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:

CODE
Dim seconds as Integer = NumericUpDown1.Value * 60
MsgBox("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:

CODE
seconds = seconds - 1
Label1.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:

CODE
If seconds = 0 then
MsgBox(TextBox1.Text)


like the original program.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No new   45 jacob 17,513 5th November 2009 - 05:00 AM
Last post by: Soviet Rathe
No new   60 -iancovenant- 6,672 13th November 2009 - 11:45 AM
Last post by: templehost
No new   61 Etherion 45,134 17th November 2009 - 06:14 AM
Last post by: Johnmck93
No New Posts   1 SamiFX 1,155 6th May 2007 - 06:03 PM
Last post by: hitmanblood
No new   33 djleli 13,781 17th September 2006 - 08:14 AM
Last post by: jlhaslip
No new   22 djleli 18,598 22nd November 2009 - 10:22 AM
Last post by: phatuis
No New Posts 13 spyshow 7,285 29th September 2004 - 10:40 PM
Last post by: odomike
No New Posts 4 Saint_Michael 3,948 29th September 2004 - 08:52 PM
Last post by: alperuzi
No New Posts   7 hansley 4,460 19th October 2008 - 04:44 PM
Last post by: bluedragon
No new   24 mayank 3,146 5th September 2009 - 04:19 PM
Last post by: iG-Jeff
No new 227 nicky1 53,351 2nd April 2009 - 10:19 AM
Last post by: random truth
No new   20 Travis18520 7,519 5th May 2009 - 04:46 PM
Last post by: iG-GenerationGirl
No New Posts   11 cse-icons 16,258 12th July 2008 - 07:55 PM
Last post by: iGuest-Giri
No New Posts   0 Shackman 4,052 4th November 2004 - 04:50 AM
Last post by: Shackman
No new   15 round 6,082 10th February 2005 - 08:28 PM
Last post by: round


 



RSS Open Discussion Time is now: 26th November 2009 - 06:20 PM

Web Hosting Powered by ComputingHost.com. Xisto.com : Honesty ROCKS! Truth Rules.