Unit1 VB
Unit1 VB
VISUAL BASIC is a high level programming language evolved from the earlier
DOS version called BASIC. BASIC stands for Beginners' All-purpose Symbolic
Instruction Code. The program codes in Visual Basic resemble the English
language. Visual Basic is a fairly easy programming language to learn.
Examples of events are clicking a command button, entering text into a text box,
selecting an item in a list box etc. Therefore, a VISUAL BASIC Program is made
up of many subprograms; each with its own program code which can be executed
independently and at the same time can be linked together in one way or another.
PROCEDURAL PROGRAMMING:
Procedural Programming can be defined as a programming model which is
derived from structured programming, based upon the concept of calling
procedure. Procedures, also known as routines, subroutines or functions,
simply consist of a series of computational steps to be carried out. During a
program’s execution, any given procedure might be called at any point,
including by other procedures or itself.
Languages used in Procedural Programming: FORTRAN, ALGOL, COBOL,
BASIC, Pascal and C.
When you fire an event, the code in the event procedure is executed,
and then visual basic performs its operations as per the instructions
written in the event procedure code. For example, in the first sample
program, when you click the 'Print' button, the click event is fired,
and then the code in the click event procedure gets executed. The
code tells Visual Basic to print a text on the form. So as a result, you
see a text printed on the form.
Example:
Write the following code in the DblClick event procedure of the form.
OUTPUT
When you double-click on the form, the DblClick event procedure of the Form
object is invoked, and then the code in the DblClick event procedure is
executed. Thus, the code instructs Visual Basic to print a text on the form.
then the code in the DblClick event procedure is executed. Thus, the code
instructs Visual Basic to print a text on the form.
Visual Basic is a high level programming language which evolved from the earlier
DOS version called BASIC. BASIC means “Beginners All-purpose Symbolic
Instruction Code”. Visual Basic is an example of a graphical-based language.
The IDE allows the programmer to create, run and debug Visual Basic programs
conveniently. IDEs allows a programmer to create working programs in a fraction
of the time that it would normally take to code programs without using IDEs.
Visual Basic, derived from the Basic language, is an object-based and event driven
programming language from Microsoft. This language is relatively easy to learn. It
enables you to create GUI (Graphical user interface) applications easily using the
Rapid Application Development (RAD) technique. The one most interesting
feature of this language is that it comes with a designer called Integrated
Development Environment (IDE). The easy-to-use tools of the IDE enable you to
easily create buttons, textbox, and other controls for your desktop application.
Visual Basic 6.0 is a very powerful programming language. It enables GUI
application development, provides access to databases and enables the creation of
ActiveX controls. In addition, Visual Basic 6 is Event-driven because we need to
write code in order to perform some tasks in response to certain events. The events
usually comprises but not limited to the user's inputs. Some of the events are load,
click, double click, drag and drop, pressing the keys and more. We will learn more
about events in later lessons. Therefore, a VB6 Program is made up of many
subprograms, each has its own program code, and each can be executed
independently and at the same time each can be linked together in one way or
another. Start a New Project Run the Visual Basic software from the list of
programs or a desktop shortcut icon.
Click "Standard EXE". Start a Standard .exe type of project. In the beginner level,
you will only learn about this type of project. Other project types on this window
are for the advanced learners. You can do a lot of things implying VB6 is giving
you enough power in your hand through these different project types.
Click "Standard EXE". Start a Standard .exe type of project. In the beginner level,
you will only learn about this type of project. Other project types on this window
are for the advanced learners. You can do a lot of things implying VB6 is giving
you enough power in your hand through these different project types.
After this, the main workspace appears where you will develop your application
with the tools in IDE (Integrated Development Environment). It is very important
to know the names of all the elements of this development environment. The tools
available here makes it very easy for you to develop an application. The VB6 IDE
provides you many tools in one place. You can add a control on the form of your
choice, set a property of an object from the Properties Window on the right hand
side, set the form layout and many more things that you can use alongside your
coding. You can even fill the ToolBox with lots of additional controls.
With VB 6, you can create any program depending on your objective. For example
you can create educational programs to teach business, economics, engineering,
computer science, accountancy, financial management, information system and
more to make teaching more effective and interesting. If you are in business, you
can also create business programs such as inventory management system, point-of-
sale system, payroll system, financial program as well as accounting program to
help manage your business and increase productivity.
Languages like Basic and Pascal depend on variables and procedures to build the
applications .This is why it is called procedural languages. The new approach is
called object programming for visual programs like Visual Basic and Visual C++
and others. In this programming approach everything (form, command buttons,
controls) is an object. The reasons for of implementing Visual Basic program are
listed as follows:
Before you can program in VB 6, you need to install Visual Basic 6 in your
computer. On startup, Visual Basic 6.0 will display the following dialog box
You can choose to start a new project, open an existing project or select a list of
recently opened programs. A project is a collection of files that make up your
application. There are various types of applications that we could create;
however, we shall concentrate on creating Standard EXE programs (EXE
means executable program). Now, click on the Standard EXE icon to go into
the actual Visual Basic 6 programming environment.
Project is a collection of files that make up your application. There are various
types of applications that we could create To exit from Visual Basic and return
to Windows is like exit from most Windows applications. There are three ways
to close the Visual Basic as stated below.
1- Click on close button icon that appears in the upper-left corner of the screen.
2- Press Alt+F4
3- Select File >Exit
a. Menu Bar: It contains a standard command like: File, Edit, View, Window, Help
menus, and specific command such as: Project, Format, or Debug menus.
c. ToolBox: it contains a collection of tools that are needed for project design.
d. form Designer: it is a window for each form to customize the designed interface
of the application. Using the form designer, the user can add controls, graphics,
and text to create the desired form appearance.
f. Project Explorer Window: it is a list of the forms and modules for the current
projects. It is a hierarchical tree- branch structure, where the project at top of tree
and other parts like forms, modules) descend from this tree.
g. Form Layout Window: The Form Layout window is a small screen. Which is
used to reposition the form of the application so that it appears in proper place
when project is run?
1. The Main Window consists of the title bar, menu bar, and toolbar. The title bar
indicates the project name, the current Visual Basic operating mode, and the
current form. The menu bar has drop-down menus from which you control the
operation of the Visual Basic environment. The toolbar has buttons that provide
shortcuts to some of the menu options. The main window also shows the
location of the current form relative to the upper left corner of the screen
(measured in twips) and the width and length of the current form.
2. The Form Window is central to developing Visual Basic applications. It is
where you draw your application.
3. Toolbox Controls Contains a collection of tools that are needed for project
design as shown in. To show the toolbox press View> toolbox icon. The user
can place the tool on form, and then work with the tool. To place the tool on
form: click on tool>draw tool to form > the tool appears on form or double
click on tool then the tool appears on form.
Label
The label control is used to display text. It is also used to label other controls.
The end user cannot edit the label text.
TextBox
The TextBox control contains characters. End-users can edit the characters
contained in the TextBox.
CommandButton
The CommandButton control is simply a button that we see in our daily-use
software. When the end-user clicks the CommandButton, the program behaves
according to the code assigned in the CommonButton.
Option Button
This control enables the end-user to select one among several options. Only one
option button among others in a group can be on at the same time. You can
name an option using the Caption property.
CheckBox
The CheckBox control is used to make a yes/no or true-false selection. You
can check more than one CheckBox at the same time that let you make multiple
choices. You can label this control using the Caption property.
VscrollBar & HscrollBar
VscrollBar and HscrollBar controls let you create Vertical scroll bar and
Horizontal scroll bar respectively.
Frame
The Frame control is used as a container of other controls. This is also used to
group different controls especially in Option Button controls when you wish to
select more than one option. The Caption property associated with it is useful to
label the frame.
ListBox & ComboBox
The ListBox control contains a number of items. The user can select one or
more items from the list. The comboBox control has the feature of ListBox and
TextBox. This control does not support multiple selections.
DriveListBox, DirListBox & FileListBox
These controls are often used together to perform file related tasks like opening
or selecting files that are stored in the secondary memory.
Timer
The Timer control is not visible on the form when you run the program. It is
used to execute lines of code repeatedly at specific intervals. The Data Control
The Data control is used for database programming.
4. The Properties Window is used to establish initial property values for objects.
The drop-down box at the top of the window lists all objects in the current
form. Two views are available: Alphabetic and Categorized. Under this box are
the available properties for the currently selected object.
BackColor and ForeColor
The BackColor property sets the background color of an object while the
ForeColor property changes the foreground color used to display text. You can
set these properties either from Properties Window or you may wish to set in
run-time.
Font
You can set the font property from the Properties Window.
Caption
It sets the text displayed in the object's title bar or on the object. 'Caption'
property of form sets the form's title text. The text to be displayed on label is
set.
Text
It sets the text in a TextBox. Example: Text1.Text = "New program" Here the
text string is assigned to Text1.Text.
The Left, Top, Width and Height properties
1. The Left Property sets the distance between the internal left edge of an object
and the left edge of its container.
2. The Top Property sets the distance between the internal top edge of an object
and the top edge of its container.
3. The Width Property sets the width of an object.
4. The Height Property sets the height of an object.
Visible
Determines whether an object is visible or hidden.
Enabled
Determines whether an object can respond to user generated events.
Example:
Text1.enabled=False
MousePointer and MouseIcon
The MousePointer property sets the type of mouse pointer over an object. The
values of MousePointer property are
0- Default
1- Arrow
2- Cross
3- I-Beam
4- Icon
5- Size etc.
The MouseIcon property sets a custom mouse icon from your files.
The TabStop property indicates whether the user can use the TAB key to give the
focus to an object. You can give focus to the objects pressing the TAB key in the
daily use software applications. The TabIndex property sets the tab order of an
object.
Example:
Command3.TabIndex = 0
Command2.TabIndex = 1
Command1.TabIndex = 2
When you press the TAB key, the focus will be given on Command3 first, then on
Command2 and at last on Command1.
Control Box indicates whether a control-menu box is displayed on the form at run
time. If this property is set to False, the Close Button, Minimize Button and
Maximize Button are not shown on the form.
ShowInTaskBar
5. The Form Layout Window shows where (upon program execution) your form
will be displayed relative to your monitor‟s screen
6. The Project Window displays a list of all forms and modules making up your
application. You can also obtain a view of the Form or Code windows (window
containing the actual Basic coding) from the Project window.
As mentioned, the user interface is „drawn‟ in the form window. There are two
ways to place controls on a form:
1. Double-click the tool in the toolbox and it is created with a default size on
the form. You can then move it or resize it.
2. Click the tool in the toolbox, then move the mouse pointer to the form
window. The cursor changes to a crosshair. Place the crosshair at the upper left
corner of where you want the control to be, press the left mouse button and
hold it down while dragging the cursor toward the lower right corner. When
you release the mouse button, the control is drawn.
To move a control you have drawn, click the object in the form window and
drag it to the new location. Release the mouse button.
To resize a control, click the object so that it is select and sizing handles appear.
Use these handles to resize the object.
Code Editor Window: Code Editor Window is used to write a VB code for an
application. For each form there is a separate code editor window. It is
displayed when user clicks on form or object in form.
To Create an Application:
The title of program includes the name of project, and when the user first starts
the program it takes a defaulted value (project1).
The following steps are required to create an application in Visual Basic 6.0:
1) Select type of project New or Exciting. A form automatically appears in the
form design .The basis for any application's interface is the form that user
should create. User can add other forms to the project (to add another form
select project menu>add form).
2) To add objects (controls) to the form use the ToolBox.
3) Set the properties for the objects through properties window.
4) Write code. The Visual Basic Code consists of statements, and declarations.
The code for an application can be written on the Code Editor window. In this
window user can view and edit quickly any of the code.
5) Run the Application. To run the application, click the Start button on the
toolbar, or press F5.
6) Stop. To stop running the application and return to visual basic program
click on stop button in tool bar.
7) Check if there is an error, return to step 3, otherwise continue.
8) Save project.
9) Exit
Events are what happen in and around your program. For example, when a user
clicks a button, many events occur: The mouse button is pressed, the
CommandButton in your program is clicked, and then the mouse button is
released. These three things correspond to the MouseDown event, the Click event,
and the MouseUp event. During this process, the GotFocus event for the
CommandButton and the LostFocus event for whichever object previously held the
focus also occur. Again, not all controls have the same events, but some events are
shared by many controls. These events occur as a result of some specific user
action, such as moving the mouse, pressing a key on the keyboard, or clicking a
text box. These types of events are user-initiated events and are what you will write
code for most often. Events common to most VB controls are described in the table
below.
Q.2 Explain the elements of the User Interface?
Ans.: The user interface is what appears in the application’s window when it runs.
It consists of various elements with which the user can interact and control the
application. The first element of the user interface is the Form.
(i) Picture Box: This control is used to display images, and the images are set
with Picture property
(ii) Label: This control displays text on a Form that the user can’t edit.
(iii) Text Box: It displays text that the user can edit.
(iv) Frame: It is used to draw boxes on the Form and to group other elements.
(v) Command Button: It is the most common element of the Windows
interface. It represents an action that is carried out when the user clicks the
button.
(vi) Check Box: It presents one or more choices that the user can select.
(vii) Option Button: Also called as radio buttons, appear in groups, and the user
can choose only one of them.
(viii) Combo Box: It is similar to the List Box control, but it contains a text Edit
field. The user can choose an item from the list or enter in the Edit field.
(ix) List Box: It contains a list of options from which the user can choose one or
more. The user can scroll the list to locate an item.
(x) The Horizontal and Vertical Scroll Bars: The users specify a magnitude by
scrolling the control’s button between its minimum and maximum value.
(xi) Timer: It is used to perform tasks at regular intervals.
(xii) File System Controls: These controls are used to add file handling
capabilities to your application.
(xiii) Image: It is similar to the Picture Box control in that it can display images,
but it supports only a few features of the Picture Box control and requires
fewer resources.
(xiv) Data: It provides point and click access to data stored in data- bases.
(xv) OLE: It is a window you can place on your Form to host documents from
other applications, such as Microsoft Word or Excel.
What do you mean by “Integrated Development Environment”?
2. Intelligence
Back Color: It sets the background color on which text is displayed or graphics are
drawn.
Font: It sets the face, attribute, and size of the font used for the text on the control.
Caption: It sets the text that is displayed on many controls that don’t accept input,
for example, the text on a Label control, the caption of a Command Button control.
Text: It sets the text that is displayed on the controls that accept user input, for
example, the TextBox control.
Left & Top: These properties set the coordinates of the control’s upper-left corner,
expressed in the units of the container.
Enabled: By default, this property’s value is True, which means that the control
can get the focus.
Immediate Window
The immediate window has three purposes. The first is for program output. You
can use the: Debug.Print command to send something to the Immediate Window.
This can be helpful when debugging some code to see what a variable's value is at
a certain point, or to provide any other useful information. The other half is to
provide immediate input. Let's say your code is running along and stops for some
reason. Perhaps a variable has the wrong value and it's causing an error. You could
stop, correct the code, and restart, but sometimes that takes too long or we can't
easily start over. You can change type any line of code into the Immediate Window
and upon hitting Enter, the code will execute. This could be as simple as selecting
a Range, changing a variables value, or anything else where you want to have the
code immediately do something. Benefit here is that doing stuff in Immediate
window doesn't affect the code that is currently paused.
The last purpose is to query something. If you put a question mark before a line of
code, the window will try to return the result. This could be used to get the value of
a variable in code that is currently running, or if you wanted to know the value of a
cell,etc.
Examples of lines in immediate window