Visual Basic Demystified -I

Introduction
Since ages, Visual Basic (VB) has been one of the most popular choices among programmers and there are many attributes to support their decision. First on the list is the ease with which one can build a large sized user-friendly application. Secondly, not much of a formal training is required to start programming in VB. Applications written in VB can run on any platform, with minimum Windows 95.

Let us now embark on our journey to explore the various dimensions of VB.

Objects and Variables
As in the real world a person is an object (a real time entity), so also VB has objects that represent some characteristics, known in technical jargon as Properties. Values can be assigned to these properties (using ‘=’ operator) as and when required. The properties are referred to by using the dot ‘.’ operator. E.g.

ObjectName.PropertyName = ValueAssigned

Objects can perform specific tasks, which we call as Methods. There are special types of actions, i.e. Functions. And functions accept pieces of information called Arguments. Objects also have special events to be captured, referred by an underscore separator ‘_’as:

ObjectName_Click

Here, Click is known as an Event of ObjectName.

Apart from objects, there are some global definitions of variables that can be used in any of the Events. The different types of variables are listed in table 1.

Type

Bytes

Suffix

Range

Boolean

2

N/A

True/False

Byte

1

N/A

0 to 255

Currency

8

@

-922,337,203,685,477.5808 to 922,337,203,685,477.5807

Date

8

#...#

1st Jan 100 to 31st Dec 9999 and time from 0:00:00 to 23:59:59

Decimal

12

N/A

-79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335

Double

8

#

-1.79769313486232E308 to -4.94065645841247E-324 for negative values

4.94065645841247E-324 to 1.79769313486232E308 for positive values

Integer

2

%

-32,768 to 32,767

Long

4

&

-2,147,483,648 to 2,147,483,647

Single

4

!

-3.402823e38 to -1.401298E-45 for negative values

1.401298E-45 to 3.402823e38 for positive values

String

N/A

$

Two billion characters

Variant

N/A

N/A

N/A

Table 1: Variable Types

Variables can be declared using some keywords (table 2) that decide the scope of the same.

Keyword

Interpretation

Dim

Used to create variables along with keyword ‘As’ to specify variable type

ReDim

Reallocates the memory location to the previously defined or dynamic array variables

Static

The variable retains its value during procedural calls

Private

The scope of this variable is in the current form or module

Public

These are global variables

Table 2: Variables Scope

Variable declaration:
Three of the commonly used methods to define variables are

Dim name As String
Dim age As Integer

Dim address 


The first declaration specifies name as a string variable and age as an integer variable. The second declaration defines address as a variant, which can assume any value during run-time.

Dim Grad1, Grad2, Grad3 As Integer

As shown in the third method of declaration, we can separate the variable names using comma ‘,’. However, this method can be used only if the variables belong to a common type.

Integrated Development Environment (IDE)

Let us begin our first project. We select Standard EXE when the first window pops up.



A new form appears in front of us with some graphic controls for creating GUI. Many such forms can be created for different modules and then integrated or interlinked to create a project. A project is saved with an extension .vbp while forms are stored with the extension .frm.

Project Explorer Window

This window displays the forms and modules in a project. We can select the form/module to design the form or write code in the same. Three buttons are provided in the window to view the code, design the form or see various forms and modules in the project. Refer figure 2.



Toolbox Window

The toolbox (Figure 3) consists of various tools and controls. These controls are also called as objects. Sometimes, this toolbox is not visible. In that case, we need to select Toolbox option from the View menu.

Other than those visible on the screen, we can even add a few more controls (.OCX) through Project Menu -> Components.



Properties Window

The property window (figure 4) deals with object properties that we had earlier talked of. Several properties are displayed in this window along with their values, which can be altered as required. Some of the properties are mentioned in table 3.



Property

Interpretation

Name

This is the name of the object or form. It will be used whenever one calls the object events, methods or changes its properties. The nomenclature follows a specific rule as mentioned in table 4.

Appearance

If set to Flat, it paints controls and forms without any visual effect.

If set to 3D, it gives visual effects to controls and forms.

Backcolor

This property enables us to choose the background color of an object or form. It has two options:

  1. Palette – Range of all colors and their shades; and
  2. System – Shows colors commonly used in Windows.

Colors in the System bar can be changed only through proper configuration of the control panel.

BorderStyle

Sets the border style for an object.

Enabled

When we need to put off the controls of objects, we disable the object. If required, one can put Enabled to ON and hence restrict the controls to the user.

Font

The default font is MS Sans Serif. It is preferable to use commonly available fonts so that installing our software on some other PC does not disturb the appearance.

Height

Width

This property is used to change the height and width of a form or control. The controls are by default measured in twips and 1,440 twips make up an inch.

You can change the ScaleMode from twips to inch or any other measuring unit by changing the ScaleMode.

Index

This is used to create an array of objects. The objects are then referred by their unique index numbers. This renders flexibility of using the same code while programming.

Left

Top

These properties define the top and left position of the form. These properties are also known as X and Y properties.

MousePointer

When the mouse is dragged on an object, the mouse pointer assumes different shapes as defined in this property. The default value is 99 – Custom.

TabIndex

This defines the sequence of focus when the user presses TAB key. The first value assumes 0. This can help us define the sequence of operation if the user prefers to operate through keyboard.

ToolTip Text

This property is used to display some text when a mouse is placed over the object. The general appearance of the text is yellow as background color and black as foreground color.

Visible

This property hides/unhides the control or form. It is different from Enable property, which keeps the control visible but denies access.

Table 3: Some commonly used properties of objects

Prefix

Objects

Example

prj

Project

PrjCalculator

frm

Form

FrmAddition

cmd

Command Button

CmdSaveAs

txt

TextBox

TxtName

lbl

Label

LblName

lst

Listbox

lstTemperature

cbo

Combobox

CboChoice

opt

Option Button

OptChoice

img

Image

ImgCapture

fra

Frame

FraPersonal

tmr

Timer

TmrMain

dat

Data

datEmployeeRec

 Table 4: Conventional method of naming objects

Building a Form
Before placing different objects on a form and writing code for them, let us first view the properties of the form that make a difference in the form’s appearance. These properties are visible in the Property Window (see Figure 4)

Name of the form (in the Property Window, here Form1) should reflect the purpose of the form, normally preceded by a prefix frm (e.g. frmMath for mathematical calculations.)

The Caption property helps us to title our form, which appears next to the icon.

A normal form appears with a blue screen with minimize, maximize and close button. With the help of BorderStyle property, we can change the appearance of the form.

BorderStyle

Border

Sizable

Caption

Icon

Control Box

0- None

No

No

No

No

No

1- Fixed Single

Yes

No

Yes

Yes

Yes

2-Sizable

Yes

Yes

Yes

Yes

Yes

3-Fixed Dialog

Yes

No

Yes

No

No

4-Fixed ToolWindow

Yes

No

Yes,
Slimmer

Yes

No

5-Sizable ToolWindow

Yes

Yes

Yes,
Slimmer

Yes

No

Table 5: BorderStyle Property

Apart from BorderStyle, the manner in which the form shall be displayed initially is also important. The WindowState property assumes either Normal or Minimized or Maximized as its value. In the minimized state, the form is seen in the windows task bar.

Position of the form also plays an important role, which is defined by StartUpPosition property.

StartUpPosition

Position

0-Manual

The form position is as defined in the Left and Top properties.

1-CenterOwner

The form is centered on the form that has called it.

2-CenterScreen

At the center of the screen.

3-WindowsDefault

Can place anywhere on the screen. This is selected by default.

Table 6: StartUpPosition Property

With this information, you should now be able to view different objects and their properties. This would be useful in creating good real-time applications.




Added on December 1, 2007 Comment

Comments

Post a comment