Lecture 1 - Introduction to Starting Visual Basic
Table of contents
1.0 Introduction to VB - About the Language1.1 Introduction to VB - Why Use VB?
1.2. Introduction to VB -Summary of the advantages and disadvantages of VB
1.3 Introduction to VB - Further Reading
2.0 The Visual Basic IDE
2.1 The Visual Basic IDE - Further Reading
3.0 Building VB Applications
3.1 Building VB Applications - VB Apps and Projects
3.2 Building VB Applications - The VB Design Cycle
3.3 Building VB Applications - Standard VB Controls
3.4 Building VB Applications - Writing VB Code
3.5 Building VB Applications - Further Reading
1.0 Introduction to VB - About the Language
Microsoft Visual Basic VB is an extremely simple, yet powerful programming language, which is useful for building many kinds of Windows applications. It is capable of constructing almost any program that any other Windows-capable programming language can produce, however its power and the reason why it has sold millions of copies world-wide is the ease with which you can build practical business oriented software solutions.
Learning the VB language does not restrict you to using Microsoft Visual Basic to build applications. Visual Basic for Applications VBA, a subset of the VB language that can be found in many applications such as Microsoft Office and Visio, enables you to customise the host application, relatively painlessly. And VBScript or Microsoft Script, a subset of VBA, which is fast becoming a popular scripting language for World Wide Web sites. In this course we focus mainly on VB, however, in the last lecture we shall take a brief look at VBA and VBScript
VB Concepts and a Short History
As its name suggests VB is a Visual programming language . This makes the construction of programs a simple 3 step process.
Build the User Interface , which will consist of a combination of windows, menus, buttons etc. Drawing the UI using the mouse much like a DTP program draws a page.
Customise the properties of the interface, so that it has the look and feel that you are after.
Attach Program Code to make the interface work in the way you desire.
We shall take a closer look at this process later in section 3.2 of this lecture.
The language that you use to attach code to your program is BASIC quite obvious really!!. BASIC has had a long history in the computer industry, it was designed in the 1970s as a language for teaching programming. It has the advantage that its syntax is very similar to English prose, making it very easy to pick up.
Microsoft has brought BASIC bang up to date with VB adding many new features to make windows programming that much easier. VB1 was released in approximately 10 years ago and was one of the first of a new breed of visual programming languages which provided ease of use through graphical design techniques. By version 5, database support was added through the use of the Microsoft Jet Database Engine which enabled data to be shared across other Microsoft products such as Access. VB5 added native code compilation, which heralded a huge leap forward in the speed which VB programs executed. Finally VB6, the latest version, offers features such as support for the design and implementation of ActiveX components, strong Internet connectivity, access to remote databases, and much more
VB is Event Driven , which means that programs dont follow the sequential path of traditional languages such as Pascal . Instead snippets of sequential code are associated with certain events such as the clicking of a button. Then when the program is run, events will occur or are said to fire in response to changes in the environment such as the ticking of the computers internal clock or by intervention from the user such as clicking a button. When an event fires the associated sequential code is then executed. Once this is completed the system will wait until the next event fires, and the whole process repeats.
Example of Visual, Event Driven Programming
In this example we see a dialog box which contains the usual attributes of a window:

FIGURE 1, SCREEN SHOT OF DIALOG BOX WITH LABEL TEXTBOX AND BUTTONS
These have been drawn onto the window or form as it is known in VB just like you draw boxes in a drawing program. First you select the object such as a text box from a palette and then you click and drag on the form where you want the control to appear.
Then once the interface is built, code can be attached to different events. For example, each button will have a Click event, which will fire whenever the user presses it. To this event we can associate some code which in the case of the OK button saves the contents of the text box and closes the window, and in the case of the cancel Button just closes the window. In workshop 1 we shall work through a practical example of this.
VB is also an Object-Oriented OO programming language, this means that most things in a VB program are designed as objects. In traditional programming languages data is stored quite separately from the operations which can be performed on that data. For example, you may implement a queue structure in Pascal as an array, with a collection of procedures and functions that operate on the queue, returning data such as the head of the queue or performing actions such as adding data to the end of the queue. Not so in an OO programming language.
An object is a construct that stores data and the operations that can be performed on the data together. So we could define a queue object which internally holds the data and makes a series of operations, which return the head and add to the tail, visible to he outside world. The beauty of OO is that objects abstract the complex internal behaviour from the programmer, it is almost as if an object is a black box with a series of connections that operate the box. All we are concerned with are the connections, not with how the box actually implements its behaviour. So in our example other programmers need never know that internally the queue is stored as an array, all they need to understand are the operations which access the data.
This makes programming conceptually easier, because a lot of the complex behaviour is hidden away. It also means that the internal behaviour of the object can be modified without affecting the rest of the program, as long as the external interface interface in the sense means the objects connections with other objects remains the same. This means that we could modify our queue object to store the data as a linked list rather than an array. We would obviously need to change the operations of the object, but as long as they keep the same names and arguments and return the same data, then the rest of the program will not need to be modified.
At this point in the course we shall not concern ourselves with how an object is defined internally, we shall only be interested with its external behaviour, which is defined by 3 factors, the information it makes available, the operations it can perform and how it reacts to its environment. In VB terms an object
So to learn and understand any type of object you need to study each of its
The combination of Properties, Methods and Events which an object make visible to the outside world is known as its interface which is the way in which programmers can manipulate the object. Not to be confused with a user interface which is the method by which some objects enable end-users of your application to manipulate the object.
There are many examples of objects throughout VB, for example an individual command button is an object. A command button object can be used when we understand its
Even this simple example shows the benefits of OO, we dont need to worry about how a button is implemented, how it is drawn on the screen or how a Click event fires. All we need to know is at a much simpler abstract level e.g. where to position the button on screen. Also because we need not understand the blood and guts of the button Microsoft can upgrade it to make it look smarter or more efficient, as long as the external interface remains the same.
As we will see throughout this course there are many objects defined by VB. All interface controls, text boxes, buttons etc., are objects, there are also objects which are not visible, such as the clipboard object which has properties to retrieve what is on the windows clipboard, and methods to store data there.
1,1 Why Use VB?
As we have already stated VB is an excellent language both to learn windows programming techniques and to implement practical business tools. It is suitable for building database applications for personal or workgroup use, as well as graphics intensive applications, and even Internet applications. But there are situations when VB is probably not the most appropriate language, for example if you want to do lots of very low level programming, you may be better advised to use C++.
There are even occasions when VB will not produce a satisfactory solution. For example VB applications are platform dependent, that is they will only run under windows, so if you want to develop for the Apple Mac or Linux then you should look at a platform independent language such as Java.
In previous versions of VB speed of the final program which was produced was a major problem. Unlike languages such as Visual C++ and Delphi that produced compiled code, VB was interpreted to a great extent at run-time. But as we mentioned earlier this was rectified in VB5 so that you now have the option of producing executable programs that have highly optimised native compiled code.
1.2 Summary of Advantages and Disadvantages
Advantages:
The VB language is simple and structured much like English prose making for a much shallower learning curve
Large applications can be implemented in a short space of time.
Disadvantages
Older versions of VB were slow in comparisons to other languages such as Delphi . This was rectified in VB5.
VBs simplicity can actually make some applications difficult to implement. In some circumstances it may be more appropriate to choose an alternative language.
1.3 Introduction to VB -Further Reading
MS 98 Chapter 1 Pages 3-4
Jones Chapter 1 Pages 1-6
Norton Chapter 1 Pages 2-11
Siler Chapter 1 Pages 12-16
2.0 The Visual Basic IDE
Programming 10 years ago meant sitting at a terminal using an archaic text editor to bang out complex code, then using a command line compiler that if you were lucky would build your applications. However, if you were unlucky and had bugs in your code, as usually happened, then you were presented with error messages that were at best unhelpful and at worst gibberish. Luckily with VB Microsoft has produced an environment, which makes the process of building apps much easier. VB provides us with an easy to use interface with facilities to build compile and debug code in a graphical understandable way.
The VB Integrated Development Environment IDE is an extremely powerful tool, and it pays to understand it. Investing a short amount of time pays dividends because the IDE has many laboursaving features. It has developed with each new version of VB however each of the following is present in all versions newer that VB4:
2.1 Further Reading
MS 98 Chapter 2 Pages 14-17
Sybex Chapter 1 Pages 3-31
Jones Chapter 2 Pages 7-16
Norton Chapter 5 Pages 161-183
3.0 Building VB Applications
To learn how to build a VB application there is no better way than to roll up your sleeves and get your hands dirty. So in this section we introduce you to the foundations of VB development, you will then finish the lecture by completing the associated exercise and workshop sheets, which will take you step by step through the construction of you first VB app.
3.1 VB Applications and Projects
When building your VB app you will inevitably have to create many files, so to make the management of these files easier VB enables you to create Project files. These are container files, given the file suffix .vbp, that point to every file your app may need. Each project can contain any of the following:
There are other types of file, which a project can store, but only those above are used in the remainder of this course.
You will find each of the files that you use in your project listed in the project window project explorer to make managing projects easier, as shown in figure 3. Also because each form, module, MDI parent form and class is saved in its own file it is possible to reuse existing code in other projects.

FIGURE 3, SHOWING A CLOSE UP OF THE PROJECT EXPLORER
3.2 The VB Design Cycle
When building software, the actual implementation should form only a small part of the whole software engineering process, proper design before and rigorous testing after are vital to the smooth construction of efficient code. However VB does aid the implementation phase by breaking it down into a seven-stage process:
Create a new project,
Create a new form,
Add controls to the form,
Modify the properties of each control on the form,
Write the code to make the form operate,
Repeat steps 2-5 to add more forms or alternatively build MDI forms, modules or class files,
Save the project,
This takes two steps, first save the project file then save each form, module, MDI form and class,
Run and debug the project
In exercise sheet 1 we shall introduce exactly how this process is achieved, but for the rest of this lecture we are going to look at what foundation knowledge you need before you can do this.
3.3 Standard VB Controls
So you have started a new project, got your first form file, now you need to add some controls. Here we list the standard palette of VB Controls that are found in the toolbox to the left of the screen, as shown in figure 4. Balloon help is available so that you can hover the mouse pointer over an icon to get a brief idea of what it does, and you can get help by selecting a tool and pressing the F1 key.
FIGURE 4, SHOWING THE TOOLBOX
3.4 Writing VB code
The last piece of the puzzle which you must understand, to be able to build your first VB app is how to write the code which plugs everything together. As was stated earlier in section 1.1 VB is an event driven language, that is you build your app by attaching code to the events of the controls that you are interested in. The Event Procedure is the construct used to associate a block of code with an event. Creating event procedures is easy !!, all you do is
1. Select the control with which you want to associate some code,
2. Double click on it, and
3. A code window will be displayed with the Procedure Stub of the default event. To write code for a different event than the default click on the procedure drop-down list box in the top right of the code window, you can then select the appropriate event.
A procedure stub defines the name and arguments for the event, for example
Private sub cmdClose_click
End sub
Would be the procedure stub for the click event of a control called cmdClose. VB will always write the stub for you, and you should leave it alone, as modifying the stub will prevent the procedure from firing when the event occurs. The part you modify is the blank line in between the Private and the End Sub lines.
To give another example, if we wanted to add an event procedure for the Click event of the OK button in Example used in Section 1.1 we would double click on the button, and the code window would appear as shown in figure 5.

FIGURE 5 SHOWING AN EXAMPLE CODE WINDOW
frmName.caption = "Hello"
would display the word Hello in the title bar of frmName. Alternatively we could call a VB procedure, e.g.
MsgBox "Hello"
would create a simple dialog box as shown in figure 6.

6 SHOWING AN MSGBOX WITH CAPTION HELLO
3.5 Further Reading
MS 98 Chapter 2 Pages 18-29Sybex Chapter 1 Pages 33-37





