Visual Basic?
Visual Basic is a powerful application development tool developed by Microsoft with the advent of the Windows environment. It is used for creating customized windows based programs.
Visual Basic is a descendant of BASIC which was often the first language that programmers used to learn in order to become familiar with programming basics before switching to any other powerful language
In text based programming, the programmer controls the user interface by encoding instructions but in a Visual programming environment, the programmer can quickly design the user interface by drawing and arranging the screen elements. This feature helps the programmer to concentrate on developing the applications functionality rather than spending time on the repetitive programming tasks. Thus, Visual Basic is often referred to as a Rapid Application Development (RAD) tool.
Visual Basic is not just a language but it is an Integrated Development Environment. IDE is basically a term commonly used in the programming world to describe the interface and environment where you develop, run, test, and debug your applications.
Designing a Program
There are certain structured approaches that one has to keep in mind while creating a computer program, that are as follows:
- Plan how the program task should work.
- Design the user interface.
- Write the program’s code.
- Test and debug the program.
- Documentation and implementation of the program i.e. putting it to use.
- Flexibility in program for future modification.
Editions of Visual Basic
Before you start designing applications using Visual Basic , you need to know about the editions it comes in:
The Learning Edition contains the tool that lets you create Windows applications.
The Professional Edition is for computer professionals and includes advanced features such as tools to develop ActiveX and Internet controls.
The Enterprise Edition includes the entire feature found in the Professional Edition, plus includes Microsoft Visual Source Safe for source code control and, the Automation and Component Manger.
Common Terminology
The following table lists some of the key terms used in Visual Basic. You’ll be learning more about each term later in the course.
• Design time
• Run time
• Controls
• Objects
• Properties.
• Methods
• Forms
• Event
• Event driven programming.
• Project
Getting Started
• Click the Start button on the Task bar.
• Select Programs, then Visual Studio 6.0, and then click on Microsoft Visual Basic 6.0.
• The Project Wizard will open when you start Visual Basic for the first time.
• New Project dialog box will appear as shown in figure where you can select any type of project to develop an application. The window has three tabs:
New: It provides various types of projects to choose form.
Existing: It allows you to select an existing project, which could be a sample project included with Visual Basic, or one created by you in the past.
Recent: It allows you to select from the most recently used projects. The tab is similar to the Existing tab, but it presents you with a list of the existing projects you have worked on recently, instead of all of the existing projects.
By selecting a project from the New Tab, Visual Basic creates the foundation of the application The New Tab presents several Project templates as discussed below
• Standard EXE
• ActiveX EXE, ActiveX DLL
• ActiveX Controls
• Active X Document EXE, Active X Document DLL
• VB Application Wizard, VB Wizard Manager
• Data Project
• DHTML Application
• IIS Application
• Addin
• VB Enterprise Edition Controls
The Integrated development environment (IDE) Features
• The integrated development environment or IDE which is an important part of Visual Basic. It’s appears where you put together your applications
• The IDE consist of a number of elements, including those shown in the figure when a project type is selected.
The IDE is made up of a number of components as discussed below:
Toolbar
• The buttons in the Toolbar represents the control you can include in any program you develop.
• The Toolbar is typically the main focus of your attention as you begin a new project and choose controls to depicts the options, procedures, and activities you are planning in your application.
ToolBox
• The Toolbox contains the objects and controls, that can be, added to forms to create the user interface of simple as well as complex applications.
• Even additional controls can be added to the Toolbox by using the Components command on the Project menu .
Form Window
• In the middle of the screen, between all the toolboxes and other windows, the form designer is placed, which is actually a workspace where you actually design the visual layout of the form and the controls that are placed in the form window.
• It is also know as the Form Layout window or Form Designer window.
Project Explorer Window
• The Project Explorer window as shown in figure is quite similar to Windows Explorer, which allows you to expand and collapse the subfolders (The collection of files is called Project).
• Project Explorer is a quick reference to various elements – forms, classes, and modules in your project.
Properties Window
• Properties Window displays the various characteristics (or properties) of the selected object, (each and every form in an application is an object).
• Each and every control for example a command button that appears on a form is also an object and all characteristics of an object are called its properties.
• In Properties window you will see a list of properties belonging to an object.
Code Editor Window
• In Visual Basic, the editor is called the Code Window as shown in figure. Code window can be opened by double-clicking on a form or control in the Form layout window.
• The code can be either associated with a form in your project or contained in a separate code module
• The Code Editor contains two drop down lists at the top of the window: the Object list contains a list of all the controls contained in the form and the Procedure list displays all the events for that control.
Event –Driven Programming
• In traditional or "procedural" applications, the application itself determines, which portions of code execute and in what sequence.
• Execution starts with the first line of code and follows the coding sequence defined in the application. At times controls are transferred to other parts of the program through statements (e.g GoSub, GoTo) and Procedures.
• Where-as applications written in Visual Basic are event-driven, In an event-driven application, the code doesn't follow a predetermined path — it executes different code sections in response to events.
• Events can be triggered by the user's actions, by messages from the system or other applications, or even from the application itself.
• The sequence of these events determines the order in which the code executes; thus the path differs each time the program runs.
Objects
• In Visual Basic, an application is a combination of Objects like Forms and Controls, Procedures that respond to Events, and other general-purpose procedures.
• Objects in Visual Basic are basically considered as a combination of code and data, which is treated and controlled as a unit. Command buttons and other controls on a form are objects.
• These objects form a major part of a Visual Basic application, which allows the user to enter the data as well as view the results. Each form in a Visual Basic project is a separate object.
• An object may contain other objects e.g.; form can contain any number of objects – Command buttons, a label, and a text box and when an object is created a default name base is assigned on its object type, such as Form1, Command1, or Text1.
• To have better control on the object the Name property of each control is changed to a name that describes the purpose of the control.
Controlling Objects
• To control an object, properties, methods, and events are used. Properties consist of the object’s data, setting and attributes. Text, Caption, and Name are common example of properties.
• Some properties can be set at design time by using the Properties Window, while other properties that are not available at design time are set by coding the properties at run time.
There are two ways to set the object’s property –
• The Properties window is used to set object properties at design time. The properties that are established at design time, are used as initial settings each time applications runs.
To set the value of a property at design time:
• In the Form Designer window, select the form or control for which you want the property to be .
• In the Properties Window, select the property you want to set
• Type or select the property setting you want, as shown in figure.
Methods
• Methods are procedures that operate on the object or that the object performs on data. Methods cause an object to perform an action or task. Move and SetFocus are common examples of methods.
• Methods are part of objects like Properties and perform the actions you want.
• It can also affect the value of properties, e.g. list boxes have a List Property that can be changed with the Clear method to remove all of the items from a list or the AddItem method to add a new item to a list.
• Methods can be called in several different ways. The syntax used for calling a method depends on whether the method returns a value and if that value will be used by your application.
Object.Method[arg1, arg2..]
The above mentioned syntax is used with methods that don’t return a value, or if you don’t want to use the value returned by the method
• Optionally, you can even place parentheses around the arguments when you want to use the value returned by the method Generally, parenthesis is used when the method appears to the right of an equal sign, the syntax is as follows:
Variable=Object.Method([arg1, arg2, ...])
Events
An event is an action recognized by a form or control. Any action performed by pressing a key on the keyboard or clicking a mouse for which code can be written is an event procedure that runs whenever the event occurs.
Events occur as a result of the user actions or program code, or they can be triggered by the system.
Depending on the event the corresponding procedure is executed, but some events recognized by one object may not be the same as recognized by other objects.
Some of the events which are used very frequently are the Load event in a Form and the Click event of a command button. Some of the events are listed below as shown in figure.
Modules
Code in Visual Basic is stored in three kinds of modules- Class module, Form module, and Standard module.
• Each Standard, Class and Form module can contain:
• Procedure: A Sub, Function, or Property procedure contains pieces of code that can be executed as a unit.
• Declarations: You can place constants, variable, procedure declarations at the module level of form, class or standard modules.
Standard Modules
• Standard modules(.BAS filename extension) are containers for procedures and declarations commonly used by other parts of the application.
• They can contain global or module level declarations of constants, variables, external procedures, and global procedures.
Form Modules
• Form modules (FRM filename extension) are the foundation of any Visual Basic application.
• They contain the textual description of a Form and its Controls including their property settings. They can contain Form-level declarations of types, constants, variables, and external procedures that handle events, and general procedures.
• Forms are the parts of your application that are visible to users at run time.
Class Modules
• Class modules (CLS filename extension) mark up the foundation for the object-oriented programming in Visual Basic. You can write code in Class modules to create new Objects.
• These new classes can include their own customized properties, methods, and events. All of the properties and methods you create can also be used by other objects in your application.
• You can also use the New keyword to create multiple copies of your objects.
Introduction to Forms
• A Visual Basic form is used to develop the user interface. It is basically a window where you can add different elements in order to create a complete application.
• Every application you see on the screen is based on some type of form. Forms have their own properties, events, and methods, which are used to control their appearance and behavior.
• A default form, Form1 is added to the development environment when Visual Basic starts, as shown in figure.
• Visual Basic applies a default set of properties to this form and any new forms that are added to the project.
• The properties of these forms can be changed for its appearance and behavior either at design time or at run time.
Working with Form Properties
• The form’s appearance and behavior are determined through its properties, which are set in the Properties window. This chapter focuses on the main properties of the form.
The BorderStyle Property
• The Border Style Property determines how the border of a form behaves and looks.
• This property as shown in figure, is used for setting form size that can be set at design time and can’t be changed at run time.
Introduction to Form Controls
• Controls are objects that are placed within form objects. Command buttons, List boxes and Scroll bars are examples of Controls.
• Each type of control has its own set of properties, methods, and events suitable for a particular purpose.
• The toolbox contains the standard controls. The controls can be added to a form by just double-clicking on the control or by drawing the selected control on a form by dragging the mouse around the area where you want the control to be placed.
• Properties of the control can be set either at design time or at run time. The properties are set at design time by simply clicking on the control to make it the active control and then changing the settings in the Properties window (the Name property is always set at design time).
• When a control is created, Visual Basic gives a default name indicating the type of control, plus a unique integer which can be changed by starting it with a letter followed by number or underline ( _ ).
Basic Controls
Label, Text box and Command buttons are the most commonly used controls on a form.
Using Labels
• A label is a graphical control used to display text. Label displays read only text; the user cannot edit the text directly.
• The property which is set in a label control is the caption property, which can be set either at design time or run time.
• At design time you set the property by selecting it from the control’s properties window.
• At run time you can set the caption property to provide instruction or additional help to the user .
• The alignment of the text within a label control is set through Alignment property 0- vbLeft Justify, 1-vbRightJustified, 2- vbCenter ( by default text is left justified )
• Select New Project from the File menu.
• Select Standard Exe project type from the New Project dialog box.
• Select the Label control from the toolbox and draw it on the form.
• Double-click on the form. Enter the following code:
Private Sub Form_Load()Form1.Caption = "About the Institute"
Label1.Caption = "Institution Name:"
Label1.Alignment = 2
Label1.Font = "Arial Black"
End Sub
• Select Start from the Run menu. The application appears as shown in figure
• Select Save Project As from the File menu. Save the application as Sample.vbp.
Using TextBox
Text boxes are commonly used for accepting user input or for entering data. Text boxes can be used in conjunction with a Data Control to display information from a database.
• It is also used to set up database queries or to edit records in a database. The Name property of the text box begins with the ‘txt’ prefix.
• Multiple lines of text can be displayed by using the MultiLine property of the TextBox control in conjunction with the ScrollBars property.
• The MultiLine property can be set to True or False, and the ScrollBars property can be set to none (0-vbSBNone), (1-vbHorizontal), Vertical (2-vbVertical), or both (3-vbBoth).
• If the MultiLine property is set to True, the Alignment property can be used to set the alignment of text within the text box.
• Select Open Project from the File menu.
• Select Sample.vbp from the Open Project dialog box.
• Select the TextBox control from the toolbox and draw it on the right side of the Label control.
Using the CommandButton
• A CommandButton control is one of the most common control found in a windows application. A CommandButton performs a task when the user clicks on the button.
• A CommandButton control is used for begin, interrrupt or end a process. The most common event for a ComamndButton control is the click event.
• Within the CommandButton control some of the commonly used properties are - Cancel, Caption, Default, Font, Name , ToolTip Text, method is SetFocus, and Events are Click, MouseDown, MouseMove.
• Select Open Project from the File menu.
• Select Sample.vbp from the Open Project dialog box.
• Select CommandButton from the toolbox and draw it on the form .
• Select the CommandButton. In the Properties window set its Name property to cmdClose and its Caption property to Close. The CommandButton appear.
• Double-click on the Close commadbutton. Enter the following code:
Private Sub cmdClose_Click()Unload Me
End Sub
(In above code, Me is a keyword that can be used in any form to refer to itself, just as you can use the word Me to refer to yourself without having to use your name.)
• Select Start from the Run menu. .
• Save the application as Sample.vbp.
Interacting with the User using MsgBox and Input Box function
• Usually, dialog boxes are used in Windows–based applications to prompt the user for applications need before continuing or displaying any information to the user.
• One of the simplest ways to get information to and from the user is to include a message box using the MsgBox, and to query the user with the InputBox function.
Using the MsgBox Function
• Message boxes are simple and the fasts way to query the user for the simple information or to allow the user to make decisions about the program’s course.
• It displays a message and provide buttons that the user can use to respond to. The figure displays the message generated by the MsgBox function: The syntax for the MsgBox function is as follows:
MsgBox (Prompt,[button][, title] [, helpFile, context])
• Prompt: In prompt the user can store the text that contains message.
• Button: This button determines the number of and type of message box buttons as well as the type of icon that appears on the message box.
Creating an Executable File
• Select Make Project1.EXE from the File menu
• You can create an EXE file from the DOS prompt by typing the following command:
VB 32/Make project name exename
To create an Executable File Select Make EXE file from the File menu and type the file name.
To add version information
In the Make EXE File dialog, choose the Options command. In the EXE
Options dialog, type the version numbers and text for the Version
information and choose OK.
You can also create an EXE file from the DOS prompt with the following
command:
VB 32/Make project name exe name
Using Help
• Like most of the Windows applications Visual Basic has a context-sensitive help which is available to you, if you press the F1 key.
• You can copy code examples from the Help file and paste it directly onto your application.
• In the code window, you can place the cursor directly over the word for which you want Help, and press F1.
If you receive a run-time error, you can press F1 to get Help on the error message. You can of course access Help from the Help menu.