34
Lec3 P 1 isual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index If - Then Statement Select Case User defined Types Dialog boxes : Message boxes, Input boxes Menu Designer

Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

  • View
    220

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 1CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

CP2030 VBFCLecture 3

Back To Index

If - Then Statement

Select Case

User defined Types

Dialog boxes : Message boxes, Input boxes

Menu Designer

Page 2: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 2CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Syntax: If...Then... Statement

In Visual Basic the statement has two main forms, in both the condition is held inside normal (curved) brackets

Single line form 1:If...Then...If ( condition ) Then {actions when

condition is TRUE}

Example:If (iMarks<40) Then Label3.Caption = "Fail"

Page 3: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 3CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Syntax: If...Then...Else... Statement

Single line form 2:

If...Then...Else...

If ( condition ) Then {TRUE action} Else {FALSE action}

Example:If (iMarks<40) Then Label3.Caption = "Fail" Else Label3.Caption = "Pass"

This form is really present to aid backwards compatibility

It is VERY BAD practice as it is detrimental to the understandability of the code, you should NOT use it!

Page 4: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 4CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Syntax: If...Then... Statement Block FormIn C:

Students to add code

Allows more than one action (statement) as part of a condition. Allows selections to be nested

In VB: Students to add code

Page 5: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 5CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

As in C - if the result of the condition is true (non zero value) statements are executed.

Hence non zero - TRUE

zero - FALSE

Unlike C the ‘=‘ symbol is used for both assignment and as an operator.

C uses :

Students to add code

Page 6: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 6CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Example 1:If (iMarks<40) Then Label3.Caption = "Fail"

Label3.ForeColor = QBColor(4) ‘redEnd If

Example 2:

Students to add code

Page 7: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 7CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Syntax: If...Then...Else... Block Form

In C:

if (expr)

{ statements

}

else

{ statements

}

In VB - Block form:Students to add block form

Page 8: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 8CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Example:If (iMarks<40) Then Label3.Caption = "Fail" Label3.ForeColor = QBColor(4) ‘redElse Label3.Caption = "Pass" Label3.ForeColor = QBColor(1)

‘blueEnd If

Page 9: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 9CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Syntax: If...Then...ElseIf...Else.. Block Form

– Block form:If ( condition ) Then actions ... when condition is TRUE

ElseIf ( condition ) Then actions ... when condition is TRUE Else actions ... when both other

conditions are FALSE End If

– Example:If (iMarks<40) Then Label3.Caption = "Fail"ElseIf (iMarks<60) Then Label3.Caption = "Pass"Else Label3.Caption = “Merit”

End If

Page 10: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 10CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Comparison Operators

All The normal operators are available in VB

> greater than < less than= is equal to >= is greater than or equal to<= is less than or equal to<> is not equal to

NOT(….)

Page 11: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 11CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Switch Statement In C :

switch (val)

{ case 1:

statements;

break;

case 2:

statements;

break;

default:

statements;

}

Page 12: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 12CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Select Case Statement 1

In VB - simplest form of the Select statement is:

Select Case test expression Case expression list

{actions ... when expression list is TRUE}

Case Else{actions ... when all conditions are

FALSE} End Select

There are a number of forms of the expression list

Cases can be freely mixed, prior to Case Else

Page 13: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 13CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Select Case Statement 2

e.g. Select Case test expression Case expression 1 {actions ... when expression 1 is TRUE} Case expression 2, expression 3 {actions ... when expression 2 or 3 are TRUE} Case expression 4 To expression 5 {actions ...when in expression 4 To 5 range is

TRUE} Case Is = expression 6 {actions ... when test expression = expression

6} Case Is >= expression 7 {actions ... when test expression >= expression

7} Case Else {actions ... when all conditions are FALSE} End Select

Page 14: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 14CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Syntax: Select Case Statement

Students to add case syntax eg

Page 15: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 15CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Question Using the form design below, write code for

the calculate grade command button using :

(a) If Then statements ONLY

(b) Select Case statements ONLY

© What about Data Validation ?– 0-39 FAIL, 40-59 PASS, >60 MERIT

Page 16: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 16CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

User Defined Types - OverviewC++ - Data Structures

Pascal/Delphi - Records

VB - User defined types

Used to hold a group of related data under one name

eg.

Students to add examples

Page 17: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 17CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Declaration

In C In VBstruct DateType Type DateType{ iyear As Integer

int iyear; imonth As Integer

int imonth; iday As Integer

int iday; End Type};

DateType sbirthday; Dim uBithday As DateType

Page 18: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 18CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Assignment to User Defined Variables 1 ‘C’ has far more flexibility - pointers used

together with structures ( ie. Pointers to structures and offsets to access the fields of the structure.

Variables of UFDs can be assigned to in two ways:

1. To an individual element/field within the user defined type variable

uBithday.iyear = 1998

uBithday.imonth = 6

uBithday.iday = 20

Students to show input and output

Page 19: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 19CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Assignment to User Defined Variables 2

2. To the user defined type variable as a whole:

Both variables involved in the assignment must be of the same user defined type

uNewBook = uOldBook

Both uNewBook and uOldBook are of the user defined type BookType

This would assign all of the elements/fields of uOldBook to their respective element/field in uNewBook

Page 20: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 20CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

User Define Types - Detailed Example

This is a sample entry in a card index system

Title:

Author(s):

Pages:

ISBN:

Year:

Understanding & Using Visual Basic

Barron, Jonathan C

448

0-314-07155-5

1996

Page 21: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 21CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Creating a User Defined Data Type

Students to add notes

Page 22: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 22CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Declaring a User Defined Type

A user definedtype must bedeclared withinthe generaldeclarationssection of acode module

Page 23: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 23CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Syntax: Type Definition Declaration starts with keyword Type and

ends with End Type Each element/field should be defined

Example:Students to add type declaration

Page 24: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 24CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Declarations of Variables of the Type

Once a type definition is placed in the code module it is available as a type throughout the application

We use exactly the same process here as for any other variable

Dim uNewBook As BookType

Students to add other declarations

Page 25: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 25CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Question - revision

Which declarations could be made where?(i.e. at module level, form level or control handler level)

Page 26: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 26CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Nesting of Data Structures

We can nest a data structure within another data structure:

'udt declarations in a code moduleType ChargesType

cDayHire As Currency cMileage As Currency cInsurance As Currency

End Type

Type BillType uCharges As ChargesType cTotalBill As Currency cRefundExcess As Currency

End Type

BillType

uCharges

cTotalBill

cRefundExcess

cDayHirecMileagecInsurance

ChargesType

Page 27: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 27CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Accessing Nested Data Structures

Example of accessing the variable

Dim uBill As BillType

Sub Command5_Click ()‘calc component charges uBill.uCharges.cDayHire = iDaysOnHire*DAYHIRERATE@ uBill.uCharges.cMileage = (iFinishMiles-iStartMiles)

* MILEAGERATE@ uBill.uCharges.cInsurance = iDaysOnHire*DAYINSURANCERATE@‘calc total & refund/excess uBill.cTotalBill = uBill.uCharges.cDayHire

+uBill.uCharges.cMileage +uBill.uCharges.cInsurance

uBill.cRefundExcess = uBill.cTotalBill - DEPOSIT@End Sub

Remember all assignements are one line only!

Page 28: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 28CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Dialog BoxesMessage Boxes 1

Message Boxes are used when you want the user to choose from a limited number of options, or just to inform them of something

Const MB_OKCANCEL = 1Dim iResponse As IntegeriResponse = MsgBox("Message",MB_OKCANCEL,"Title")

They are used for warnings, confirmation, etc..

Page 29: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 29CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Message Boxes 2

There are a standard range of button combinations and icons available

MB_ICONSTOPMB_ICONQUESTIONMB_ICONEXCLAMATIONMB_ICONINFORMATION

They can be added together to give the required Message Box:Const MB_OKCANCEL = 1Const MB_ICONQUESTION = 32Const MB_BOXSTYLE = MB_ICONQUESTION + MB_OKCANCELDim iResponse As IntegeriResponse = MsgBox("Message",MB_BOXSTYLE,"Title")

Page 30: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 30CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Message Boxes 3

You can act upon the users response to the message box: Const MB_OKCANCEL = 1 Const MB_ICONQUESTION = 32 Const MB_BOXSTYLE = MB_ICONQUESTION + MB_OKCANCEL Const IDOK = 1 Dim iResponse As Integer

‘show the message box iResponse = MsgBox("Message",MB_BOXSTYLE,"Title")

‘check how the user exitedStudents to add remainder of code

The message box can be application or system modal

Page 31: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 31CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Input Boxes

The Input Box allows you to get information from a user, You can set the title, message, default text and position on the screen

It returns the string that the user entered when they clicked Ok

If you Cancel then an empty string "" is returned Example:Label1.Caption=InputBox$("Enter Name","Title")

Page 32: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 32CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

A Typical Drop-down menu

Menu bar

CascadingMenus

Separatorbar

Shortcuts

Checkedoption

DisabledOptions

Page 33: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 33CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

The Menu Design Window

This code gives Label1 blue text when “Blue Text” option is selected

Page 34: Lec3 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 3 Back To Index v If - Then Statement v Select

Lec3 P 34CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton

Demo of menu builder

As above example