Sign In
Link to us
Home
1. Introduction
2. Introducing VB
 2.1 VB Concepts
 2.2 Object Orientated
 2.3 VB Windows

3. Projects
4. Containers
5. Controls
6. Code
7. Forms
8. Debugging Code
9. Error Handling
10. Objects
11. User Controls
12. Data Projects
13. API
14. Distribution

Other Resources


Affiliated Links:
www.freefunfings.com


2. Introducing VB

2.1 VB Concepts

2.1.1 Environment
Visual Basic is shipped as part of the Visual Studio suite.

This package also contains applications such as Visual C++, Visual SourceSafe, Visual InterDev, Visual J++ and common components, such as Bookshelf Basics (Help).

Up to version 4, Visual Basic was designed for use with both 16 and 32 bit Windows operating systems (Windows 3.11/95/NT). In version 6, Visual Basic was designed to run only under a 32 bit Windows operating system (Windows 95/98/ME/2000/NT).

Commonly used components and commands are contained within the Visual Basic Runtime Libraries. These libraries are installed as part of Visual Basic and shipped with your application when you use the 'Packaging & Deployment' wizard to create a Setup program. They are usually called "Vbrun???.dll" and installed into the Windows/System directory. As you use third-party or custom libraries, these too will require installation by your Setup program. This is necessary so that anybody who installs your application onto a PC that hasn't got Visual Basic installed can still run it.

A cut-down version of Visual Basic is shipped with Office. This version (Visual Basic for Applications or "VBA") can be used to develop methods and functions inside Office applications. Due to licensing constraints and file size considerations, some of the functionality in Visual Basic is not present in VBA.

2.1.2 Execution
In classical programming, such as BASIC, C or PASCAL, the program execution begins at the top and runs sequentially down to the end of the main code body. If subroutines are invoked along the way, these are also dealt with sequentially. If the user presses a key on the keyboard or clicks the mouse, the program has to Scan for this input and read it from a buffer. If the program does not Scan for it, it doesn't see it.

This meant that in large complex programs, keyboard and mouse inputs could be missed when the program was 'busy' - say doing some calculations inside a subroutine where the keyboard scanning is not done. Visual Basic does not operate sequentially like this.

Visual Basic is driven by Events - pieces of code are executed in response to something happening. When you click on an application window, a Click event has occurred. The Click event has an associated subroutine which you may put code into to determine what happens if the Application window is clicked. The application is then static; doing nothing (actually, Windows automates system events during these periods, such as re-drawing forms that have been re-sized).

This concept means that a program is a collection of associated subroutines. These subroutines can be invoked as a result of an Event such as a mouse click, or can be programmatically invoked by the code. Each object that is used in Visual Basic (such as a Form or Control) has a different set of Events that are applicable.

Thus, in addition to an object having properties, such as width, height and color, an object also has Events and methods (subroutines) associated with these Events.

2.2 Object Orientated Methodology

2.2.1 Objects
An Object in Visual Basic is an elemental part of the application that has constituent characteristics that can be accessed by the application. These characteristics have special names, which will be made clear later, but for now we'll call them characteristics.
An Object's characteristics are accessed using the dot (".") modifier. For each dot in the term, you are asking Visual Basic to drill down another level. An Object's characteristic could itself be an instance of another Object.

Example:
Let's say we have a new Object called "objCar". This Object has the following characteristics:

ObjCar.CarBodyColour
       Engine
       Tyres

But, the characteristic "Engine" is itself an object, with the following characteristics:

Engine.IsDiesel
       Make
       Valves
       BHP
       Plugs

And again, the characteristic "Plugs" is an Object with the following characteristics:

Plugs.Count
      GapSetting
      Size

From the above information, we could refer to any characteristic of "objCar" within the whole range of subcategories, using the dot modifier as follows:

Access the colour of the bodywork:         objCar.CarBodyColour
Check if this car is a diesel or not:      objCar.Engine.IsDiesel
Check the gap setting for the spark plug:  objCar.Engine.Plugs.GapSetting

This philosophy could be employed for just about any subject matter you need. It forms the basis on which Visual Basic (and other languages) is designed to operate.

The structure of the Object and it's characteristics is known as an Object Model.

Exercise i:
Think of a different category and design your own Object Model for it on a piece of blank paper.

2.2.2 Class versus Instance
The Object Model defines the structure of the Object you are going to use in Visual Basic. Once you have declared this structure, it will be used with every element that you specify to be that Type.

To continue with our car example, let's assume we are writing an application to store information about different type of car. We already have the Object Model, so we can just declare new Instances of it for the different cars:

Dim FordEscord As objCar
Dim VauxhallAstra as objCar
Dim FiatUno as objCar

Note:  Note the use of the keyword Dim. This declares to Visual Basic that you want to define (or 'dimension') a new variable. The As modifier tells Visual Basic what Type you want the new variable to be. For example, if you declared a variable as follows, Visual Basic would create a variable called "strA" as a "String" type.

Dim strA As String
Each of these new Instances of the Object is assigned the framework structure that has been declared for their Type (objCar). Once they have been declared, they operate like 'photocopies' of the original and like photocopies, you can write over each 'copy' with information unique to that copy.

For example:

FordEscort.Engine.IsDiesel = "Yes"
FiatUno.Engine.IsDiesel = "No"
VauxhallAstra.Engine.IsDiesel = "No"

The Class of an Object can be thought of as the mould for the instances of that object.

2.3 VB Windows

When Visual Basic is first installed, it is configured to display various windows that help the developer to move around and program more efficiently. It is possible, however, to close these windows so that they do not appear when Visual Basic is next launched.

This is a summary of the various windows and tools available and how to access them.

2.3.1 Project Explorer
When developing in Visual Basic, you can check which Forms, Modules etc. are included within your Project using the Project Explorer. This window looks a bit like Windows Explorer - it has folders that can be expanded and contracted, each with files inside.

The Project Explorer is usually on the right-hand side of the screen, however if it isn't open select "Project Explorer" from the View menu or press Ctrl+R.

2.3.1.1 View Code
One of the buttons at the top of the Project Explorer window (see above) is the "View Code" button. Clicking this with an appropriate Project Explorer element selected (e.g. Form, Module) will display the Visual Basic Code window for that element.

The "View Code" window can also be accessed by double-clicking the object in the "View Object" window (see below).

2.3.1.2 View Object
The middle button at the top of the Project Explorer window (see above) is the "View Object" button. Clicking this with an appropriate Project Explorer element selected will display the graphical view of that element.

As mentioned above, the "View Code" window can also be accessed by double-clicking the object in the "View Object" window.

2.3.1.3 Toggle Folders
The last button at the top of the Project Explorer window is the "Toggle Folders" button. This button toggles the Project Explorer view so that it is either divided by category or divided by type.

2.3.2 Properties Window
Elements of Visual Basic, such as Forms, Modules, Controls etc. have Properties. A Property is a characteristic of the relevant element. For example, each Control is given a unique Name property, which associates it with statements within the Visual Basic code.

Each time an element is selected, it's Properties are displayed in the Properties Window. From here they can be read and/or changed.

The Properties Window is usually on the right-hand side of the screen, however if it isn't open select "Properties Window" from the View menu or press F4.

2.3.3 Toolbox
The Toolbox contains all of the controls that are currently loaded into the Project. Controls are the interactive elements of a window, such as buttons, text boxes and pictures. Visual Basic contains a basic toolbox of controls that are always available to you and additional ones that can be added as you need them.

You can also create your own custom controls, called "User Controls" or "ActiveX" controls. When developing ActiveX controls, these too will appear in the Toolbox. We will explore these in more detail later.

A Control can be included in an application by first selecting it in the Toolbox and then placing an instance of it within an appropriate container (e.g. a Form).

The Toolbox is usually on the left-hand side of the screen, however if it isn't open select "Toolbox" from the View menu.

2.3.4 Toolbars
Several toolbars are available within Visual Basic. Toolbars are shortcuts to regularly used menu options.

Toolbars can be opened from the "Toolbars" sub-menu within the View menu.

<<Page 1  :   Top  :   Page 3>>

©vbtraining.co.uk