Unit 11 Visual Basic

34
100hrs Information Technology Training © Board 100 Hours ITT Course Chapter 11 Visual Basic

Transcript of Unit 11 Visual Basic

Page 1: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

100 Hours ITT CourseChapter 11Visual Basic

Page 2: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Introduction

• Visual basic is windows based and Event Driving programming

language.

• It is an Integrated Development Environment with Multiple

Document Interface (MDI).

• It provides fully functional windows applications with all the

usual user-interface controls such as command buttons,

labels and textboxes.

Page 3: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

The VB Environment

• The above diagram shows the development environment

with all the important points labeled.

Properties window

Toolbox

Project ExplorerWindow

Form LayoutWindow

ToolboxForm Designer Window

Page 4: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

The VB Environment

VB Application – Visual Basic Project Window

To start the Visual Basic 6.0, Click on start menu, then choose Microsoft Visual Studio 6.0, then new project

Select Standard.EXE, then click open to see the default Form

Page 5: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

• VB Application – Visual Basic Project Window

The VB Environment

Double click on default form to see the source code window consists of list of objects and their associated events and procedure.

Page 6: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

The VB Environment

Source Code Window shows Object list and procedure list

Page 7: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

The VB Environment

Structure of VB Program

Private Sub< Name>()

Comment Statement(s)

Declaration Statement(s)

Basic Statement(s)

End Sub

Page 8: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

The VB Environment

A Simple Example shows first application of VB

Source code :-

Private Sub Form_Load ( ) Form1.show Print “Welcome to Visual Basic Tutorial”End Sub

Page 9: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

The VB Environment

A Simple Example shows arithmetic application of VB

Private Sub Form_Activate ( ) Print 20 + 10 Print 20 - 10 Print 20 * 10 Print 20 / 10End Sub

Private Sub Form_Activate ( ) Print 20 + 10, 20 – 10, 20 * 10, 20 / 10End Sub

Page 10: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

The VB Environment

Steps in Building Visual Basic Application

3 Steps for Creating application

Step 1: Design the Interface

Step 2: Set properties of the Controls (Objects)

Step 3: Write the events’ procedures

Page 11: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Visual Basic Application using controls and procedures

Working with Common Control

Page 12: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Working with Common Control

Private Sub Command1_Click() Sum = Val(Text1.Text) + Val(Text2.Text) Label1.Caption = SumEnd Sub

Text Box Control

Page 13: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Working with Common Control

Label ControlCaption - the text that is displayed in the label

BackColor and ForeColor - colors of the background and the text

BackStyle - Opaque or Transparent - whether the background is visible or not

Font - font and size of text

Alignment - text centered, left or right

Multiline- True or False - if True, we can have several lines of text, delimited by <CR> in the label - by default, it is set to False

Page 14: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Working with Common Control

Input name and city and display the information in a label when the Continue button is clicked. The Exit button will end execution of the program and the Cancel button (or the Esc key) will clear the fields

Text Box and Command Button Control

Page 15: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Working with Common Control

Checkbox and Option Button Control

Page 16: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Working with Common Control

Checkbox and Option Button Control Cont..

Page 17: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Working with Common Control

Assignment 1Create the Payroll form shown below. Number of hours must be entered as well as the appropriate rate. Gross salary = rate * hours. Net salary = gross salary - deductions.

Page 18: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Working with Common Control

List Box Control

Page 19: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Working with Common Control

Combo Box Control

Page 20: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Button Properties For Reference

, Command Button & labels properties Property Description

Name The name of the object so we can call it at runtime

BackColor This specifies the command button's background color. Click the BackColor's palette

down arrow to see a list of common Windows control colours, you must change this to the style property from 0 - standard to 1 - graphical

Caption Holds the text that appears on the command button.

Font Produces a Font dialog box in which we can set the caption's font name , style and size.

Tab index Specifies the order of the command button in tab order

Tool Tip Text If the mouse is held over the object a brief description can be displayed (for example hold the mouse over one of the above pictures to see this happening

Visible If you want the user to see the button/label select true other wise just press false

Working with Common Control

Page 21: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Data Types

The data type of a programming element refers to what kind of data it can hold and how it stores that

data. The various types of Data Types are -

Numeric Data

Byte, Integer, Long, Single, Double, Currency, Decimal

Non-numeric Data

String (fixed and variable), Date, Boolean, Object , Variant

(Numeric/ Text)

Visual Basic Data Types

Data Basics

Page 22: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

• If...Then...Else Statement

– Conditionally executes a group of statements,

depending on the value of an expression.

• General form:

– If condition Then [ statements ] [ Else

[ elsestatements ] ] End If

Controlling & Looping Program Flow

Data Basics

Page 23: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Controlling & Looping Program Flow

• Multiple If Conditions:

• General form:

– If condition1 Then [ statements 1] End If

– If condition2 Then [ statements 2] End If

– If condition3 Then [ statements 3] End If

Controlling & Looping Program Flow

Page 24: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Controlling & Looping Program Flow

• Select...Case Statement

– Runs one of several groups of statements, depending

on the value of an expression.

• General Form:Select [ Case ] testexpression

[ Case expressionlist[ statements ] ]

[ Case Else[ elsestatements ] ]

End Select

Controlling & Looping Program Flow

Page 25: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Controlling & Looping Program Flow• The DO WHILE Loops

– do while loop is perhaps the most common looping

statement that will put in VB programs.

– The comparison expression controls the looping

statements.Format One:

Do While (comparison test)Block of one or more VB statements

LoopFormat Two:

DoBlock of one or more VB statements

Loop While (comparison test)

Controlling & Looping Program Flow

Page 26: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Controlling & Looping Program Flow• The DO UNTIL Loops

– Whereas the DO WHILE loop continues executing the

body of the loop as long as the comparison test is true,

the DO UNTIL loop executes the loop as long as the

comparison test is false.Format One:

Do Until (comparison test)Block of VB statements

LoopFormat Two:

Do Block of VB statements

Loop Until (comparison test)

Controlling & Looping Program Flow

Page 27: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Controlling & Looping Program Flow• The FOR Loop

– For loops execute a series of one or more VB statements a fixed

number of times or until a condition is met.

– General form:

For intCounter = intStart to intEnd [step intIncrement]Block of VB statements

Next intCounter

intCounter must be a numeric variable that controls the body of the loop. An iteration is one loop cycle.

Controlling & Looping Program Flow

Page 28: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Message Box

• Msgbox function

– Message boxes are used when we want to ask the user a

question or display an error message(s) and advise the

user.The Buttons displayed in a message here

Button Layout Value Short Description

vbOKonly 0 Displays the OK button.

vbOKCancel 1 Displays the ok and cancel button.

vbAbortRetryIgnore 2 Displays the Abort , Retry , Ignore

vbYesNoCancel 3 Displays Yes , No and Cancel button

vbYesNo 4 Displays the Yes / No button

vbRetryCancel 5 Displays the retry and Cancel buttons.

Page 29: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Message Box cont..

• The Icons displayed in the message box are hereIcon on message Value Short Description

vbCritical 16 Displays critical message icon

vbQuestion 32 Displays question icon

vbExclamation 48 Displays exclamation icon

vbInformation 64 Displays information icon

Page 30: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Message Box cont..

• The syntax for use of the message box:

– Mgsgbox "TEXT", VALUE, "TITLE“

– We should get the picture shown below whatever source

code you used:

• MsgBox "The Device was not found!", vbExclamation,

"Header"

Page 31: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Input Box

• InputBox Function

– Displays a prompt in a dialog box, waits for the user to input text or

click a button, and then returns a string containing the contents of the

text box.

• General form:

– myMessage = InputBox(Prompt, Title, default_text, x-position, y-

position)

• myMessage is a variant data type but typically it is declared as string,

which accept the message input by the users.

Page 32: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Input Box cont..

• The arguments are explained as follows:

– Prompt - The message displayed normally as a question

asked.

– Title - The title of the Input Box.

– default-text - The default text that appears in the input field where users

can use it as his intended input or he may change to the message he wish

to key in.

– x-position and y-position - the position or the coordinate of the input box.

Page 33: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Input Box cont..

• Example:

– userMsg = InputBox("What is your message?", "Message

Entry Form", "Enter your messge here", 500, 700)

Page 34: Unit 11 Visual Basic

100hrs Information Technology Training © Board of Studies, ICAI

Thank youThank You