Visual Basic - Fundamentals of VB and IDE

7 minute read

Since ages, Visual Basic (VB) has been the choice of programmers and there are many attributes supporting this.  The first and foremost being the ease with which one can build large sized user-friendly application.  The second being, no much of formal training required to start programming in VB.

In this series, we aim at understanding programming from a programmers point of view. Initially called by as a programming language for hobbyist, Visual Basic has now captured the corporate world too.  Applications written in Visual Basic can run on any platform with minimum Windows 95. 

Let’s embark our journey to Visual Basic Programming.  As we proceed through the series, we shall explore the various dimensions in VB.

Objects & Variables

As in real world, person is an object (a real time entity), so does VB have objects.  These objects represent some characteristics which we technically call Properties of objects. These properties can be assigned values (using ‘=’ operator) when required.   The properties can be referred to by using a dot ‘.’ operator as:

ObjectName.PropertyName = ValueAssigned

The objects can perform specific tasks, which we call as Methods. There are special types of actions, which we call as Functions. These functions accept piece of information called Arguments.

The objects also have special events to be captured that are referred by an underscore separator ‘_’as:

ObjectName_Click

Here, Click is known as an Event of ObjectName

Besides Objects, there are some global definitions of variables, which can be used in any of the Events of Objects. There are different types of variables which are as follows:

Table 1: Variable Types

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

#...#

1 Jan 100 to 31st Dec 9999 and times 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

$

2 Billion characters

Variant

N/A

N/A

N/A

These Variables can be declared using some keywords that decide the scope of the same. They are as follows:

           

Table 2: Variables Scope

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

Variable Declaration

There are three commonly used methods to define variables:

Dim name As String

Dim age As Integer

Dim address

The first declaration declares 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 ‘,’.  But this method can be used only if the variables belong to a common type. 

Integrated Development Environment

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

Figure 1: New Project Window

<![if !vml]=>clip_image002

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


Project Explorer Window

<![if !vml]=>clip_image004


Figure 2: Project Explorer

In this window, the Project Forms and Modules are visible.  On clicking on the name of form/module we can design the form or write code in the same.

The three buttons in the window are to view the code, design the form or see various forms and modules in the project. 

Toolbox Window

<![if !vml]=>clip_image006<![endif]>Â

Figure 3: Toolbox Window

The toolbox appears as shown in Figure 3. This toolbox consists of various tools and controls that can be used.  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 add few more controls (.OCX) through Project Menu > Components.


Properties Window

<![if !vml]=>

clip_image008

Figure 4: Properties Window

The property window deals with object properties that we had earlier talked of.  Several properties are displayed in this window along with their values.  The values can be altered as required. 

Some of the properties are mentioned in Table 3:

Table 3: Some Commonly Used Properties of Objects

Property

Interpretation

Name

This is the name of object or form, which 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 visual effect

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

Backcolor

This property enables us to choose the backcolor of object or form. It has two options as

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

The colors in System bar can be changed only through proper configuration of 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.  When needed, one can put Enabled to ON and hence restrict the controls to the user.

Font

The default Font is MS Sans Serif.  It’s preferable to use the commonly available fonts so that installing our software on some other PC doesn’t disturb the appearance.

Height

Width

This property is used to change the height and the width of the form or the control.  The controls are by default measured in twips and 1,440 twips make up to 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 index numbers that are unique. This renders flexibility of using same code while programming.

Left

Top

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

MousePointer

When mouse is dragged on the object, 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 to define the sequence of operation if the user prefers to operate through keyboard.

ToolTip Text

This property is used to display some text when mouse is placed over the object.   The general appearance of the text is yellow as backcolor and black as forecolor.

Visible

This hides the control or the form, whichever is desired. This is different from Enable property.  The Enable property does keep the control visible but not accessible.

Table 4: Conventional Method of Naming 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

Building A Form

Before placing different objects on the form and writing code for them, lets first view the properties of form that make a difference in the 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

Table 5: BorderStyle Property

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

Besides BorderStyle, how the form shall be displayed initially is also important. The WindowState property assumes either Normal, Minimised or Maximised as its value.In the minimized state, the form is seen in the windows task bar.

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

Table 6: StartUpPosition Property

StartUpPosition

Position

0-Manual

The form position is as defined in the Left and the 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.

With this information, let us view different objects and their properties so that we can create good real-time applications.

 

 

 

Note: This series was first published in DeveloperIQ and was co-authored by Puneet Ghanshani with Pranjali Bakeri in 2005-2006.

Updated:

Leave a Comment