Clocks and Countdowns

As a beginning coder in the world of Visual Basic.Net, you may feel that your programs have reached a plateau. If you want to take your programming to a new level, then you may want to consider the addition of a more advanced feature for the programs you design. One of the ways to do that is with the addition of a clock or a countdown in your program.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 5
February 03, 2010
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Sure, they may not be right for all applications, but in the correct context, adding a clock or a countdown to your program can truly enhance your development. If you are considering adding a clock or a countdown to your next program, then this piece can help you to get it right, and make all of the decisions you need to make. Read on if you want to take it to the next level.

Why a clock or countdown?

You may wonder why, from a developer's point of view, putting in a clock or a countdown is a step up from your usual "find the object on the list, place the object into your program and give the object functionality" routine. Well, there are a few good reasons.

1. You can tie it to other objects. One of the stops on the way to becoming a great developer is learning to tie two objects together in order to create a function the two could not achieve separately. For example, it could work well with a trivia game with a time limit on answering questions.

2. You can give a sense of organization to a distraction-free program. Distraction-free programs are a great way to help end users focus on only on task. (That's a harder and harder thing to do in the age of multitasking). The only problem is if you have to close a distraction-free program to get basic information, like the time, then your program really isn’t that distraction-free.

3. Because sometimes it just makes sense. Let’s say that you are creating a new planner program. In that case, clocks (for setting an event to a specific time in the day) and countdowns (for reminder notices) can be an important part of the program. At least they can be if you want to make a planner with those functions. Any program with reminders or deadlines will probably involve at least one clock or countdown. It may even involve at least one of each of those elements.

Now that we have looked at three good reasons why you, as a developer, might want to learn about clocks and countdowns, let’s move on to the definitions.

Defining clocks and countdowns

What is a clock?

You probably have one sitting on your wrist, or at least on your cell phone. In a Visual Basic.Net program, the clock will get its time from the system.

What is a countdown?

A countdown is any increment that starts at its highest time and works its way down to zero. This can be an increment of time, such as counting down the 60 seconds that a user has to answer a question.

A countdown can also be used in a less traditional manner. If you only give a user a set number of something, say attempts to put in a password, then that is a countdown, too. When you think about it, a countdown is really quite a versatile tool -- one that can be implemented without the end user even knowing it was there until they hit zero.

When does a clock or countdown become superfluous?

All good things are only good in a specific set of circumstances. Sure, a clock may be a good idea, and so might a countdown if the program warrants them, but in the wrong program they can be pointless, or worse, even damage a program's usefulness to the end user.

Think about it; you probably don’t care about what time it is in the middle of a game. A time limit will not make much sense if your user is entering new data into a planner. The bottom line here is, always use your common sense when you are thinking about adding one of these elements to your program. If you need some help, try answering these questions.

  • Does having a clock or a countdown make sense in this program?
  • Will using these elements enhance or degrade the experience for the end user?
  • Do I have the right amount of space to fit a clock in this program?

Once you get past those three questions, all doubt should be removed. You will have your answer. Just be sure not to get so blinded by the idea of having a cool feature that you do not take the time to evaluate properly. Too many good programs have been ruined by the temptation of adding a new feature.

Now that you know if you should put a clock or countdown into your program, let's talk about how to do it.  

How to put a clock or countdown into your next Visual Basic.Net program

Clock 

If you want to put in a clock, just follow these steps and you can have your clock in the software quickly and easily.

Start with a text box and a timer on your form. Add a text box that is large enough to contain each of the digits for the clock. Add one text box for the hour, one for the minutes, and one for the seconds -- unless you do not want to have seconds; in that case, add the ones for hour and minutes only.

Once you get your text boxes in place, you can work with the time control. Just set the interval of your control to 1,000. That way your time controls will flip over once per second. Now you can add the rest of the code for the timer. Here is the code that you will need to use.

For the hours you should use this snippet:

txtHours.text = left(Now(), 2).

For the next section you need to use this for the minutes: 

txtMinutes.text = mid(Now(),4,2).

Finally, it is time to take care of the seconds with this little snippet of code:

txtSeconds.text = mid(Now(),7,2).

Just remember to rename your labels the way that you have named them when you placed them on the site.

Countdown

If a countdown is more like what you desire, then these steps can help you to get it done.

  1. Put in a timer and a label.

  2. Name then as you like. In this case they have been labeled Timer1.interval and Label1.Caption. You will need to make any changes to the names in your code if you name them differently.

  3. Then add the code you see below.

 

1.

Private Sub Form_Load()

2.

Timer1.Interval = 1000

3.

Label1.Caption = "100"

4.

End Sub

5.

 

6.

Private Sub Timer1_Timer()

7.

if label1.caption = 0 then

8.

timer1.enabled = false

9.

msgbox ("100 secs is up")

10.

else

11.

Label1.Caption = label1.caption - 1

12.

end if

13.

End Sub

(Code courtesy of the following site: http://www.daniweb.com/forums/thread14312.html)

Now that you have your instructions, you can put the clock or the countdown of your dreams into your next program. There is no fuss or stress involved. Just be sure that you name your variables and elements properly, otherwise you will create an error.

Guidelines on where to put a clock

Putting a clock into your program is easy enough. Putting it into the right place is a bit more complex. Placing an element is easy if you follow a few general guidelines.

For a left set clock

* Keep it in line with your farthest item on your left. The program has guides built in for a reason.

* Keep it to the top or bottom of the page.

For a center set clock

* Be sure that you are in the true center of the screen. Users will notice if it is lopsided.

* Keep it at the bottom, unless you want the clock to be the center of attention.

For a right set clock

* Keep it in line with the farthest left set edge; use the guides.

* Make sure that you can get the clock in without having to make a user side scroll.

Of course, there are a wide variety of special situations where you can get it in a unique location, but that will depend on your overall layout. Just be sure that you take a second look at the program before you compile it.

Now you have all of the details for creating the best of clocks and countdowns in your Visual Basic.Net program. Best of luck with it!

blog comments powered by Disqus
VISUAL BASIC.NET ARTICLES

- Basic Form Properties and Modality in VB.NET
- Multiple Document Interfaces in Visual Basic
- Visual Basic for Beginners
- ASP.NET Image to PDF with VB.Net
- MySQL in ASP.NET: Mono using VB.NET
- AsyncFileUpload File Type and File Size Vali...
- Visual Studio: Adding Functionality and Style
- Clocks and Countdowns
- User-defined Functions using Visual Basic Ap...
- Understanding Object Binding in VBA
- Mastering the Message Box
- Testing a Windows Forms Application
- Using Visual Basic.NET Features to Code a Wi...
- Correcting Code in a Windows Forms Applicati...
- Write Readable Code and Comments for Windows...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials