Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Creating A Timer Program, Using Visual Basic 2005
rvalkass
post Apr 6 2006, 08:43 AM
Post #1


apt-get moo
Group Icon

Group: [MODERATOR]
Posts: 2,053
Joined: 28-May 05
From: Hertfordshire, England
Member No.: 7,593
Spam Patrol



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
kody
post Oct 27 2006, 05:59 PM
Post #2


Newbie
*

Group: Members
Posts: 1
Joined: 27-October 06
Member No.: 32,332



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
post Sep 25 2007, 07:17 PM
Post #3


Newbie
*

Group: Members
Posts: 2
Joined: 25-September 07
Member No.: 50,629



i made another topic for my question:

http://www.trap17.com/forums/index.php?showtopic=51758


hope you could help me, sorry
Go to the top of the page
 
+Quote Post
de4thpr00f
post Nov 22 2007, 05:29 AM
Post #4


Member [Level 2]
*****

Group: Members
Posts: 76
Joined: 21-November 07
Member No.: 53,412



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
post Nov 22 2007, 04:26 PM
Post #5


Privileged Member
*********

Group: [HOSTED]
Posts: 572
Joined: 13-December 06
Member No.: 35,271



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
post Mar 17 2008, 10:16 AM
Post #6


Trap Double Mocha Member
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



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
post Mar 20 2008, 09:29 AM
Post #7


Newbie [Level 1]
*

Group: Members
Posts: 10
Joined: 9-February 08
Member No.: 57,617



good job
Go to the top of the page
 
+Quote Post
FLaKes
post Mar 20 2008, 06:34 PM
Post #8


Trap Grand Marshal Member
***********

Group: [HOSTED]
Posts: 1,134
Joined: 19-May 05
From: Mexico
Member No.: 7,234



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


Super Member
*********

Group: [HOSTED]
Posts: 416
Joined: 26-January 08
Member No.: 56,881



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.