Working with Visual Basic Controls – Visual Basic
- What are intrinsic controls?
- What are Objects in Visual Basic?
- Properties of Controls in Visual Basic.
- Events made by Controls in Visual Basic.
- Methods handling Controls in Visual Basic.
- What is Program Statement in Visual Basic?
- Visual Basic Object Names
- The importance of Naming Controls.
- Object Naming Convention.
- Basic Controls of Visual Basic.
What are Intrinsic Controls?
An intrinsic control is a powerful tool you use to create objects on a Visual Basic form. With them, you can add buttons, check boxes, labels, and text boxes to your programs. Some controls are used on virtually every application. Others are only used when a specific need arises. Several of these controls can display or edit data from a database. You can use them to see files on your hard drive right from your program. You can even read a database! These basic controls are intrinsic controls, and they’re available in every edition of Visual Basic 6.
There are 20 controls which are built in to VB. These controls are called intrinsic controls. Every one of the intrinsic controls are available to every VB program you write. When you create a “New” VB project, all 20 of the intrinsic controls will show up in the Toolbox. This is true for all versions of VB.
Before writing each event procedure in visual programming you have to add one of the appropriate visual basic intrinsic control form the toolbox.
By using only the intrinsic controls, you can create powerful programs. To select a control, just click it.
The intrinsic controls are available whenever you use Visual Basic. During design time, you can access them from the Toolbox (see Figure 1.1).
Table 1.1 lists the intrinsic controls.
|Label||Displays text on a form|
|Frame||Serves as a container for other controls|
|CheckBox||Enables users to select or deselect an option|
|ComboBox||Allows users to select from a list of items or add a new value|
|HscrollBar||Allows users to scroll horizontally through a list of data in another control|
|Timer||Lets your program perform actions in real time, without user interaction|
|DirListBox||Enables users to select a directory or folder|
|Shape||Displays a shape on a form|
|Image||Displays graphics (images) on a form but can’t be a container|
|OLE Container||Enables you to add the functionality of another Control program to your program|
|PictureBox||Displays graphics (images) on a form and can serve as a container|
|TextBox||Can be used to display text but also enables users to enter or edit new or existing text|
|CommandButton||Enables users to initiate actions|
|OptionButton||Lets users select one choice from a group; must be used in groups of two or more|
|ListBox||Enables users to select from a list of items|
|VscrollBar||Enables users to scroll vertically through a list of data in another control|
|DriveListBox||Lets users select a disk drive|
|FileListBox||Lets users select a file|
|Line||Displays a line on a form|
|Data||Lets your program connect to a database|
What are Objects in Visual Basic?
An object is a type of user interface element you create on a Visual Basic form by using a toolbox control. (In fact, in Visual Basic, the form itself is also an object.) You can move, resize, and customize objects by setting object properties. Objects also have what is known as inherent functionality — they know how to operate and can respond to certain situations on their own. (A list box “knows” how to scroll, for example.) You can customize Visual Basic objects by using event procedures that are fine -tuned for different conditions in a program.
Properties of Controls in Visual Basic
A property is a value or characteristic held by a Visual Basic object, such as Caption or Fore Color. The properties of the objects are set to the default values when the object in created. Properties can be set at design time by using the Properties window or at run time by using statements in the program code. In code, the format for setting a property is:
Object. Property = Value
Object is the name of the object you’re customizing. Property is the characteristic you want to change. Value is the new property setting.
Command1.Caption = "Hello"
Could be used in the program code to set the Caption property of the Command1 object to
The look of the object is decided by the values assigned to its properties. Thus, the user customizes an interface object provided by the visual development environment using properties.
Events of Controls in Visual Basic
A major part of the interaction between people in everyday life is in the form of event and response to events.
For example, when a door bell rings we are ready to welcome someone. The ringing of door bell is the event and the action of opening door to welcome someone is the response to that event.
In Visual basic application development, since it seeks to emulate people’s everyday life, it’s application also creates events and makes response to those events. Some few examples of events in visual basic programming are:
- Pressing a key on keyboard
- Clicking with a mouse
An event procedure is a block of code that runs when a program object is manipulated. For example, clicking the first command button in a program executes the Command1_Click event procedure. Event procedures typically evaluate and set properties and use other program statements to perform the work of the program.
An event driven application operates by responding to the user events.
The example of Math Calculator (we did previously) is a typical example of an event driven application. The application starts up and then waits for the user to perform an action. When the user clicks on one of the buttons, the buttons recognizes the click and executes some code in response.
For example, clicking on a number button will display the number in the text box. Similarly, clicking on the = button will display the result of the current operation. Events like methods need additional information in the form of arguments.
Methods handling Controls in Visual Basic
A method is a special keyword that performs an action or a service for a particular program object. It is built into the interface object and can be executed as required.
For example, consider a list box. A list box object will provide a method called AddItem. This method is to be executed each time an item is to be added to the list box.
AddItem is referred to as the Method. Similarly, most controls provide a method called Move that can be used to reposition the control during the course of the execution. This feature is required very often in applications that provide animation.
In code, the format for using a method is
Object. Method Value
Object is the name of the object you are working with. Method is the action you want the object to perform. Value is an optional argument to be used by the method.
For example, this statement uses the Add Item method to put the word Check in the List1 list box:
What is Program Statement of Visual Basic?
A program statement is a combination of keywords, identifiers, and arguments in the code that does the work of the program. Visual Basic program statements create storage space for data, open files, perform calculations, and do several other important tasks.
Example of Program statement to Calculate the sum of two number is :
Private Sub cmdAdd_Click()
‘Display the sum of two numbers
Call Add(2, 3)
Private Sub Add(num1 As Single, num2 As Single)
‘Display numbers and their sum
picResult.Print “The sum of”; num1; “and”; num2; “is”; num1 + num2
Visual Basic Object Names
Programmers typically create names for their objects that clearly identify the purpose of the object and the toolbox control that created it. For example, you might give the name lblInstructions to a label that displays user-operating instructions. (In this case, lbl stands for the Label control, and Instructions describes the label’s purpose).
The importance of Naming Controls:
A control’s name is one of its most important attributes because you literally refer to a control by its name whenever you want it to do something. Names are so important that every time you put a control on your form, Visual Basic automatically gives a name to it. If you add a CommandButton, Visual Basic names it Command1; if you add a TextBox, it’s automatically named Text1.
However, naming controls like this can be confusing. For example, if you add six CommandButtons to your form, Visual Basic will name them Command1, Command2, Command3, and so on. If you need 100 buttons, Visual Basic will name the last one Command100. How are you supposed to remember what Command67 does? The trick is, rather than let Visual Basic name your controls automatically, you should do it yourself.(will be posting about control array of command button soon)
Name a control
- After you add a control to a form, make sure that it’s selected (it has a box at each corner and side when it’s selected).
- In the Properties window(click to view image), click the control’s name in the right column.
- Delete the current name and add the name you want.
A better name for a control is one that tells not only what type of control it is, but also what it does within your program. Can you see the value here? If you consistently give your controls descriptive names, you’ll always know what they do. Naturally, there is a convention you can use to help you with this.
Object Naming Conventions
This table lists the naming conventions for objects created by the 20 standard Visual Basic toolbox controls. (Form and menu objects, which you use often but which are not in the toolbox, are given the prefixes frm and mnu respectively.) Whenever you use more than five or six objects on a form, use this table as a naming guide:
|directory list box||Dir||dirSource|
|drive list box||Drv||drvTarget|
|file list box||Fil||filSource|
|Horizontal scroll bar||Hsb||hsbVolume|
|vertical scroll bar||Vsb||vsbTemperature|
In our previous articles we covered about ‘Visual Basic Development Environment’ , ‘Fundamental Visual User Interfaces‘ and ‘Event Driven Programming’. Besides of those aspects of Visual Basic now let’s explore in depth the basic Windows controls: the controls you’ll use most often in your applications because they are the basic building blocks of typical rich client-user interfaces. Rather than looking at controls’ background and foreground color, font, and other trivial properties, we’ll be looking at the properties unique to each control and see how these properties are used in building functional, rich user interfaces.