Visual Basic Project

| May 7, 2012 | 0 Comments

visual basic project: alarm clock

Learn how to create an alarm application for your system tray.

One of the cool things about Visual Basic is that it’s very easy to create quite sophisticated looking applications with only a few lines of code. Here I’ll show you how to create a program that sits in your system tray and runs a small alarm timer.  It looks way more complicated than it is.

To get started on this application, choose File > New Project and select the Windows Form application, call the project MyAlarm and click Ok. On the form, add a button and a label. Set the Form1 text property to My Alarm and set the Button1 Text property to “Minimise to tray”. Set the Label1 Text property to “Click to minimize to tray, then run application to select alarm time”, set its AutoSize property to False, set its Size to 150,40 and set its TextAlign property to MiddleCenter.

step 1

You’ll need some extra objects so, from the Tools list locate the NotifyIcon tool in the Common Controls and drag and drop it onto the form. It will appear in the container area below the form. Add the Timer from the Components list and a ContextMenuStrip from the Menus & Toolbars collection. Select NotifyIcon1 and set its icon property to point to an icon file from your system – it needs to be a  .ico format file. Set the Text property to read My Personal Alarm and, still in the NotifyIcon1’s properties, select the ContextMenu property and set this to ContextMenuStrip1 which is the context menu strip you just added. Make sure that the Visible property for NotifyIcon1 is set to True.

Click the ContextMenuStrip icon below the form and a small context menu will appear on your form. Don’t worry about its placement as it won’t be staying there. Set the first option in the strip to read 10 Minute Alarm and the second one to read Exit.

visaual basic alarm step 2

Double click the 10 Minute Alarm option and, between its Sub and End Sub statements type this code:

Dim myalarm
myalarm = 10
Timer1.Interval = myalarm * 1000
Timer1.Enabled = True

Return to the ‘ContextMenuStrip’, double click the Exit entry and, between its Sub and End Sub statements type:

NotifyIcon1.Visible = False
End

Double click the Timer1 icon and between its Sub and End Sub statements type:

Timer1.Enabled = False
MessageBox.Show("Your 10 minutes is up!", "Alarm Reminder", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

Double click Form1 and between its Sub and End Sub statements type:

NotifyIcon1.Visible = False

Double click Button1 and between its Sub and End Sub statements type:

NotifyIcon1.Visible = True
Me.Hide()

Double click the NotifyIcon1 icon and between its Sub and End Sub statements type:

Me.Show()
NotifyIcon1.Visible = False

code for alarm clock in visual basic

Save you project and run it to test it.

When you run it you will see a small form appear on the screen. Click the button to minimize the form to the system tray where your icon now appears. If you hold your mouse over the system tray icon you’ll see the text tool tip appear. To set the timer, right click the system tray icon  and select the 10 minute alarm. Note that selecting the 10 minute alarm, right now, runs a 10 second alarm – it’s been set to a short length of time so you can tell if the program works or not. When the alarm time expires, a reminder appears on the screen. Click the Ok button to close the message box.

The program remains in the system tray so that you can set the alarm for a further period of time or click Exit to close it. When you exit the program, the icon is removed and the program closed.

Later, when you’re happy with the program, set the timer to a 10 minute interval by editing the code to read:

Timer1.Interval = myalarm * 1000 * 60

Save and build the project and you can then run it to use it as a short term timer application any time you need one. You can also adapt the basic functionality of this project to run a range of handy applications from your computer’s system tray.

Tags: , , , , , , , ,

Category: Visual Basic

About the Author ()

Leave a Reply