Lecture Notes
Programming Standards - Naming Controls
Introduction
Why Bother With Standards
Naming Controls
Introduction
Objectives
This guide will help
you to: -
· understand the need for programming standards
· understand how to name VB controls properly
Why Bother with Standards
To read and understand program code easily, it must be written consistently to conform to agreed coding standards. This means...
- naming controls and variables properly
- commenting code
- declaring variables properly
- always specifying variable datatypes
- defining variables with the smallest possible scope
- using uppercase letters for constants
- code indentation
Programmers familiar with the coding standards can understand their own code and other programmer's code much more easily if the code adheres to the standards. For a software development company, adhering to a standards ensures consistency, uniform development and ultimately leads to lower development and maintenance costs.
Naming Controls
For a control name to be meaningful in Visual Basic, the name must immediately convey at least two essential pieces of information about the control:
-
what it is
-
what it is used for
If we look at some of VB's default control names...
Text1, Command1, Option1
...for each one we can tell from the name - what it is , but we cannot tell - what it is used for .
Naming conventions can help us with this. To name a control we use a three letter prefix for the first part of the name, then for the rest of the name we use something descriptive: - something that describes what the control is used for.
Here are some examples.
|
' this is a textbox for a user to type in their first name |
|
' this is a textbox for a user to type in their last name |
|
' this is a command button for closing the program |
Of course, to understand and use naming conventions for controls you need to know the three letter prefix for each control. Below is list of the prefixes for the more common controls.
Control |
Prefix |
|
Form |
frm |
Command Button |
cmd |
TextBox |
txt |
Label |
lbl |
CheckBox |
chk |
OptionButton |
opt |
Frame |
fra |
Shape |
shp |
Line |
lin |
Image |
img |
Listbox |
lst |
Vertical Scrollbar |
vsb |
Horizontal Scrollbar |
hsb |
Timer |
tmr |
So, when naming a control, you need to use
the three letter prefix first, then the rest of the name
should relate to the function of the control - it should
describe something about it, so when you come across the
name in your code, you know exactly which control it is.
~~Activity~~
Activity A |
Try this flip card activity
to see if you can remember the correct prefixes
|
Here as some more examples
Examples:
- What would you name command button that ends a program when
clicked?
cmdExit or cmdEnd or something similar
- What would you name a text box that expects a user to
type in their first age?
txtAge or something similar
- What would you name a label that displays the height of
a room
lblRoomHeight or something similar
Can you remember how to change the names of controls?
You have to select that control and then type in the name you want in the
properties window. Look at the picture below showing the name of a command button
being set to cmdSubmit .

~~Activity~~
Activity B |
What do you think I named the command buttons shown in the picture?
Answer
What about the controls on the form below?

Answer |
One final point on naming controls. You only need to name the controls that you are going to refer to in code. For example, quite often you will have labels on your form. Most of the times these labels will be static and will never change, so it is pointless naming them. Of course, if you do have a label that the program needs changes dynamically, then you need to name it properly. Frames are similar. You only need to name them if you refer to them in the code.
~~Activity~~
Activity C |
Here is part of a program that calculates the area of a rectangular lawn. Can you guess the names I used for the...
- the two textboxes
- the picture box
- the frame
Answer
|
Fini
|