Wednesday, 6 July 2011

Introduction To Visual Basic 6.0

What is Visual Basic 6.0?
Visual Basic is a tool that is used by more number of developers than any other tool. Visual Basic has been the choice of developers for various good reasons. So many small and big companies use Visual Basic for developing various types of applications. One of the key factors that contributed to the success of Visual Basic is its ease of use.

When Windows Programming (writing programs that run on Windows OS) was very tough and was confined only to a few people who were good in C and C++, Visual Basic hit the market and changed the way one would look at windows programming. Visual Basic made windows programming so simple, even a novice started writing one or two programs for windows in Visual Basic.

What you can do with Visual Basic 6.0?
Since then Visual Basic has grown considerably. The following are the areas that are supported by Visual Basic 6.0.

Standard Windows Application
Simple to complex windows applications can be developed in Visual Basic. Visual Basic provides access to all system components such as printer and clipboard. Visual Basic allows developer to use reusable components, such as ActiveX controls. And developers have always cheered it.

Visual Basic creates standard .EXE file that you can distribute and deploy on any machine. VB allows developer to access windows API, which gives all the power of windows.

Database Application
Visual Basic allows you to create front-end portion of Client/Server applications, and application servers in three-tier client/server applications. You can access any database using ODBC and OLEDB interfaces.

ActiveX Component
Visual Basic allows you to create reusable software components based on ActiveX technology.

Internet Application
Visual Basic allows you to develop application that can run on Internet and Intranet. Support for Internet application has been enhanced in Visual Basic 6.0 by adding two new project types – DHTML application and IIS application.

Next, we will understand various editions of Visual Basic.

Visual Basic Editions
Visual Basic is available in three versions, each geared to meet a specific set of development requirements.

The following are the three editions and what they provide to developers. The editions are discussed in the order of features.

Learning Edition
Allows programmers to easily create powerful applications for Microsoft Windows and Windows NT. It includes all intrinsic controls, plus grid, tab, and data-bound controls.
Professional Edition
Provides a full-featured set of tools for developing solutions for others. It includes all the features of the Learning edition, plus additional ActiveX controls, the Internet Information Server Application Designer, integrated Visual Database Tools and Data Environment, Active Data Objects, and the Dynamic HTML Page Designer.

Enterprise edition
Allows professionals to create robust distributed applications in a team setting. It includes all the features of the Professional edition, plus Back Office tools such as SQL Server, Microsoft Transaction Server, Internet Information Server, Visual SourceSafe, SNA Server, and more.

Note: Visual Basic 6.0 is a part of Visual Studio 6.0


Starting Visual Basic IDE
Visual Basic provides IDE (Integrated Development Environment) which provides developers all the tools they need to develop applications.

To start Visual Basic from Windows:

1. Click Start on the Task bar.
2. Select Programs.
3. Select Microsoft Visual Studio 6.0 and then Microsoft Visual Basic 6.0

When you start Visual Basic IDE, you are prompted to select the type of project - more on this later in this chapter.

Components of IDE
Visual Basic’s IDE has a collection of components. Each component has a specific task. For example, Project Explorer is used to display the components of the project. And properties window allows you to view & change properties.

The following are the components available in Visual Basic IDE.

Menu Bar
Displays the commands you use to work with Visual Basic. Besides the standard File, Edit, View, Window, and Help menus, menus are provided to access functions specific to programming such as Project, Format, or Debug.

Context Menus
Contain shortcuts to frequently performed actions. To open a context menu, click the right mouse button on the object you're using. The specific list of shortcuts available from context menus depends on the part of the environment where you click the right mouse button. For example, the context menu displayed when you right click on the Toolbox lets you display the Components dialog box, hide the Toolbox, dock or undock the Toolbox, or add a custom tab to the Toolbox.

Context menu is also called as popup menu.


Toolbars
Provide quick access to commonly used commands in the programming environment. You click a button on the toolbar once to carry out the action represented by that button. By default, the Standard toolbar is displayed when you start Visual Basic. Additional toolbars for editing, form design, and debugging can be toggled on or off from the Toolbars command on the View menu.

Toolbars can be docked beneath the menu bar or can "float" if you select the vertical bar on the left edge and drag it away from the menu bar.

Toolbox
Provides a set of tools that you use at design time to place controls on a form. In addition to the default toolbox layout, you can create your own custom layouts by selecting Add Tab from the context menu and adding controls to the resulting tab.

Project Explorer Window
Lists the forms, modules and other components of the current project. A project is a collection of files, which you use to build an application.

Properties Window
Lists the property settings for the selected form or control. A Property is a characteristic of an object, such as size, caption, or color.

Object Browser
Lists objects available for use in your project and gives you a quick way to navigate through your code. You can use the Object Browser to explore objects in Visual Basic and other applications, see what methods and properties are available for those objects, and paste code procedures into your application.

Form Designer
Serves as a window that you customize to design the interface of your application. You add controls, graphics, and pictures to a form to create the look you want. Each form in your application has its own form designer window.

Code Editor Window
Serves as an editor for entering application code. A separate code editor window is created for each form or code module in your application. Code window contains two dropdown list boxes, One for the list of objects in the form and another for available events of the selected object.
Form Layout Window
The Form Layout window allows you to position the forms in your application using a small graphical representation of the screen.
Immediate, Locals, and Watch Windows
These additional windows are provided for use in debugging your application. They are only available when you are running your application within the IDE.

Note: You can also add features to the Visual Basic interface by using a program called an add-in. Add-ins, which are available from Microsoft and third-party developers, can provide features like source code control, which allows you to support group development projects.

Environment Options
Visual Basic provides a great deal of flexibility, allowing you to configure the working environment to best suit your individual style. You can choose between a single or multiple document interface, and you can adjust the size and positioning of the various Integrated Development Environment (IDE) elements. Your layout will persist between sessions of Visual Basic.

SDI or MDI Interface
Two different styles are available for the Visual Basic IDE: Single Document Interface (SDI) or Multiple Document Interface (MDI). With the SDI option, all of the IDE windows are free to be moved anywhere on screen (I recommend this for beginners); as long as Visual Basic is the current application, they will remain on top of any other applications. With the MDI option, all of the IDE windows are contained within a single resizable parent window.
To switch between SDI and MDI modes:

1. Select Options from the Tools menu.
The Options dialog box is displayed.
2. Select the Advanced tab .
3. Check or uncheck the SDI Development Environment check box.
Visual Basic then informs you that change will take place from next invocation of Visual Basic.
4. Close Visual Basic IDE and restart it to get it in the mode you want.

Note: You can also run Visual Basic from command line using /sdi or /mdi parameter to put IDE in the selected mode.

Docking Windows
Many of the windows in the IDE can be docked, or connected, to each other or to the edge of the screen. These include the Toolbox, Form Layout Window, Project Explorer, Properties window, Color Palette, and Immediate, Locals, and Watch windows.

When IDE is in MDI interface, windows can be docked to any side of the parent window; with SDI they can only be docked beneath the menu bar. Docking capabilities can be toggled on or off for a given window by selecting the appropriate check box on the Docking tab of the Options dialog box , invoked using Tools->Options.

Note: you can turn docking on or off by checking or unchecking Dockable option in Context menu.
First Application in Visual Basic
Let us create a simple application to understand the steps required to develop a simple Visual Basic application. The following is the step-by-step procedure to create an application that displays the name of the programmer (mine now, yours in your case) and quits when user clicks on quit command button.

A Visual Basic application is developed in three steps:

• Create the user interface
• Change properties
• Write code
In the first step, we lay down control on the form and arrange them in the way we want.
In the second step, we change properties of controls and form to required objects.
In the third step, we write code for events.

Let us now follow the same pattern and create user interface first.

Creating user interface
Our first application needs only one control – command button. Select command button in Toolbox and drag it and drop it on the form at the required location. Here are the required steps.

1. Invoke Form designer by double clicking on Form in Project Explorer, if designer is not already displayed. (or)
Select the icon for command button in Toolbox. The background of the icon becomes gray.
2. Go to form. The moment you enter into the form the mouse pointer changes to cross hair (plus sign).
3. Place the cross hair at the bottom of the form and drag mousepointer (means hold down the left button and move the mouse pointer). While you move mousepointer you see a gray rectangle.
4. Once you dragged from the required location to required location, release the mouse button.
5. Control appears at that location with the size that is same as the gray rectangle.
After user interface is created then proceed to next step.

Changing properties
Once the required controls are placed on the form, we have to change the properties of the control to get the required look and behavior.

Each control has a collection of properties. Each property represents an attribute of the control. For example, Name property identifies the name used to reference the control in the code and so on.

To change the properties of control (and form), we have to use Properties Window, which allows you to change properties by displaying properties on left and values on the right

The following are the steps required to change properties of command button and form.

1. Invoke Properties Window for command button by pressing F4 after selecting command button.
2. In Properties Window select Caption property and enter “ &Exit “ (without quotes) on the right of it.
3. Select Name property and change it to cmdExit.
4. Then click anywhere on the form (to unselect command button) and press F4 to invoke Properties Window for Form.
5. Select Caption property and change it to First Visual Basic Application.
6. Select Name property and change it to frmFirst.

Note: If a list of predefined values is available then Properties Windows displays a list on the right.
That’s it. Second step is also through. At the end of this your form designer should look like .

Note: Character “&” in the Caption property identifies the character to be underlined. The character that is underlined could be used to select the control with ALT key. That means, if you have Caption property of a command button set to &Exit then pressing ALT+E will select the command button
Writing Code
The final step is writing code for events. Just before we proceed further, let us understand the event to which we need to respond.

• We need to respond to Click event of the form, as we want to display the name whenever user clicks on the form.
• We need to respond to Click event of command button (cmdExit) as we have to terminate program when user clicks on the command button.

Here are the steps to write code for these two events.

1. Select form and press F7 to invoke code window.
-or-
Double click on the form to invoke code window.
2. Code window displays Load event on the right (as that is default event for form). Select Click event using the dropdown list and write code as shown in figure 1.6.
3. Double click on the command button to invoke code window. By default Click event is selected. Write the following code.

Private Sub cmdExit_Click()
Unload Me
End Sub
Listing 1.1: code for Exit command button.

Print is a statement that displays the given data on the form.

Unload statement unloads the specified object. In this case we are unloading me, which is a reference to current form. That means when you click on cmdExit, current form is unloaded. As the current form is the only form in the application, the application is terminated.

Saving Project
When you save a project, Visual Basic prompts you to enter filenames in which it has to save components, such as Form of the project and then the project.

Each component is save in a separate file and project is saved in a separate file.

Save the project using following steps.

1. Select File->Save Project
Or
Click on Save Project icon in Toolbar.
2. When Visual Basic prompts you to enter filename for form, select directory in which you want to save the form file and enter filename. By default the name of the form will be the name of the file. The default extension is .FRM (form).
3. After entering file name Click on Save button to save form.
Then it prompts you to enter name of the file for project.
4. Enter a filename. Visual Basic adds extension .VBP (Visual Basic Project).
After saving project, the project explorer looks identical .

5. Click on Exit button to terminate application.

Test Run
Now, run the application using any of the following methods.

• By pressing F5
• By clicking on Start icon in Toolbar
• By selecting Run menu and Start option.

Click on the form to test whether it is displaying the name. And click on it a few times.

That’s all you have for now. You have just created a simple Visual Basic application and tested it. From this you can understand how simple it is to write applications in Visual Basic. Well, Visual Basic is really simple but at the same time it is very powerful.

Through out this book, I will show you how to develop several applications. Each application will show you how to implement a particular concept. All that you have to do is, understand the concept and try the sample application on your system. If you are successful then try to enhance the application.
In this chapter we have seen about Visual Basic 6.0. We have developed a single application to understand the process involved in developing an application.

In the next chapter we will understand more about controls.