DCA201 Notes Unit 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Course- DCA

Subject- GUI-Programming in Visual Basic


Subject Code –DCA201 Sem- II

Unit I

What is Visual Basic?

Visual Basic is a third-generation event-driven programming language first released by


Microsoft in 1991. It evolved from the earlier DOS version called
BASIC. BASIC means Beginners' All-purpose Symbolic Instruction Code. Since then Microsoft
has released many versions of Visual Basic, from Visual Basic 1.0 to the final version Visual
Basic 6.0. Visual Basic is a user-friendly programming language designed for beginners, and it
enables anyone to develop GUI window applications easily.

In 2002, Microsoft released Visual Basic.NET(VB.NET) to replace Visual Basic 6. Thereafter,


Microsoft declared VB6 a legacy programming language in 2008. Fortunately, Microsoft still
provides some form of support for VB6. VB.NET is a fully object-oriented programming
language implemented in the .NET Framework. It was created to cater for the development of
the web as well as mobile applications. However, many developers still favor Visual Basic 6.0
over its successor Visual Basic.NET.

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.
Control Structures In Visual Basic 6.0:

Control Statements are used to control the flow of program's execution. Visual Basic supports
control structures such as if... Then, if...Then ...Else, Select...Case, and Loop structures such as
Do While...Loop, While...Wend, For...Next etc method.

If...Then selection structure

The If...Then selection structure performs an indicated action only when the condition is True;
otherwise the action is skipped.

Syntax of the If...Then selection

If <condition> Then
statement
End If

e.g.: If average>75 Then


txtGrade.Text = "A"
End If

If...Then...Else selection structure

The If...Then...Else selection structure allows the programmer to specify that a different action is


to be performed when the condition is True than when the condition is False.

Syntax of the If...Then...Else selection

If <condition > Then


statements
Else
statements
End If

e.g.: If average>50 Then


txtGrade.Text = "Pass"
Else
txtGrade.Text = "Fail"
End If

Nested If...Then...Else selection structure

Nested If...Then...Else selection structures test for multiple cases by


placing If...Then...Else selection structures inside If...Then...Else structures.

Syntax of the Nested If...Then...Else selection structure

You can use Nested If either of the methods as shown above

Method 1

If < condition 1 > Then


statements
ElseIf< condition 2 > Then
statements
ElseIf< condition 3 > Then
statements
Else
Statements
End If

Method 2

If < condition 1 > Then


statements
Else
If < condition 2 > Then
statements
Else
If < condition 3 > Then
statements
Else
Statements
End If
End If
EndIf

e.g.: Assume you have to find the grade using nested if and display in a text box

If average > 75 Then


txtGrade.Text = "A"
ElseIf average > 65 Then
txtGrade.Text = "B"
ElseIf average > 55 Then
txtGrade.text = "C"
ElseIf average > 45 Then
txtGrade.Text = "S"
Else
txtGrade.Text = "F"
End If

Select...Case selection structure

Select...Case structure is an alternative to If...Then...ElseIf for selectively executing a single


block of statements from among multiple block of statements. Select...case is more convenient to
use than the If...Else...End If. The following program block illustrate the working
of Select...Case.

Syntax of the Select...Case selection structure

Select Case Index


Case 0
Statements
Case 1
Statements
End Select

e.g.: Assume you have to find the grade using select...case and display in the text box

Dim average as Integer

average = txtAverage.Text
Select Case average
Case 100 To 75
txtGrade.Text ="A"
Case 74 To 65
txtGrade.Text ="B"
Case 64 To 55
txtGrade.Text ="C"
Case 54 To 45
txtGrade.Text ="S"
Case 44 To 0
txtGrade.Text ="F"
Case Else
MsgBox "Invalid average marks"
End Select
Procedures & functions in Visual Basic

Last modified July 6, 2020

In this part of the tutorial, you will learn Visual Basic procedures & functions.

We use procedures and functions to create modular programs. Visual Basic statements are
grouped in a block enclosed by Sub, Function and matching End statements. The difference
between the two is that functions return values, procedures do not.

A procedure and function is a piece of code in a larger program. They perform a specific task.
The advantages of using procedures and functions are:

 Reducing duplication of code


 Decomposing complex problems into simpler pieces
 Improving clarity of the code
 Reuse of code
 Information hiding

Procedures

A procedure is a block of Visual Basic statements inside Sub, End Sub statements. Procedures do


not return values.

Option Strict On

Module Example

Sub Main()

SimpleProcedure()

End Sub

Sub SimpleProcedure()
Console.WriteLine("Simple procedure")
End Sub

End Module

This example shows basic usage of procedures. In our program, we have two procedures.
The Main() procedure and the user defined SimpleProcedure(). As we already know,
the Main() procedure is the entry point of a Visual Basic program.
SimpleProcedure()

Each procedure has a name. Inside the Main() procedure, we call our user


defined SimpleProcedure() procedure.

Sub SimpleProcedure()
Console.WriteLine("Simple procedure")
End Sub

Procedures are defined outside the Main() procedure. Procedure name follows


the Sub statement. When we call a procedure inside the Visual Basic program, the control is
given to that procedure. Statements inside the block of the procedure are executed.

Procedures can take optional parameters.

Option Strict On

Module Example

Sub Main()

Dim x As Integer = 55
Dim y As Integer = 32

Addition(x, y)

End Sub

Sub Addition(ByVal k As Integer, ByVal l As Integer)


Console.WriteLine(k+l)
End Sub

End Module

In the above example, we pass some values to the Addition() procedure.

Addition(x, y)

Here we call the Addition() procedure and pass two parameters to it. These parameters are two
Integer values.

Sub Addition(ByVal k As Integer, ByVal l As Integer)


Console.WriteLine(k+l)
End Sub
We define a procedure signature. A procedure signature is a way of describing the parameters
and parameter types with which a legal call to the function can be made. It contains the name of
the procedure, its parameters and their type, and in case of functions also the return value.
The ByVal keyword specifies how we pass the values to the procedure. In our case, the
procedure obtains two numerical values, 55 and 32. These numbers are added and the result is
printed to the console.

Functions

A function is a block of Visual Basic statements inside Function, End Function statements.


Functions return values.

There are two basic types of functions. Built-in functions and user defined ones. The built-in
functions are part of the Visual Basic language. There are various mathematical, string or
conversion functions.

Option Strict On

Module Example

Sub Main()

Console.WriteLine(Math.Abs(-23))
Console.WriteLine(Math.Round(34.56))
Console.WriteLine("ZetCode has {0} characters", _
Len("ZetCode"))

End Sub

End Module

In the preceding example, we use two math functions and one string function. Built-in functions
help programmers do some common tasks.

In the following example, we have a user defined function.

Option Strict On

Module Example

Dim x As Integer = 55
Dim y As Integer = 32

Dim result As Integer


Sub Main()

result = Addition(x, y)
Console.WriteLine(Addition(x, y))

End Sub

Function Addition(ByVal k As Integer, _


ByVal l As Integer) As Integer
Return k+l
End Function

End Module

Two values are passed to the function. We add these two values and return the result to
the Main() function.

result = Addition(x, y)

Addition function is called. The function returns a result and this result is assigned to the result
variable.

Function Addition(ByVal k As Integer, _


ByVal l As Integer) As Integer
Return k+l
End Function

This is the Addition function signature and its body. It also includes a return data type, for the
returned value. In our case is is an Integer. Values are returned to the caller with
the Return keyword.

You might also like