Subject: Ministry of Higher Education And Scientific Research

advertisement
Ministry of Higher Education And
Scientific Research
Babylon University
College of Material Engineering
Subject: Programming in Visual Basic
Polymers Engineering Department
2015-2016
Language
Class: Second Year
Lecturer: Hussein Mohammed Salman
Sixth Lecture : Conditional Statements (part II)
Some Examples about the conditional statements:
Example1:Design a project to characterize the input symbol ? In this example we
will use the conditional statement select case to write the codes:
1st Stage : Design Form
In this stage , we need some tools such as two command buttons , two labels and
only one text box, as bellow:
2nd Stage : Set Properties
Object (Tool)
Command1
Text1
Label1
Property
Setting
Name
Caption
Font
Font Size
Name
Text
Alignment
Caption
Font
cmdTest
Test
Times New Roman
12
txtSymbol
This Lecture allowed on: www.uobabylon.iq.edu
2-center
Input The Symbol
Times New Roman
1
Ministry of Higher Education And
Scientific Research
Babylon University
College of Material Engineering
Subject: Programming in Visual Basic
Polymers Engineering Department
2015-2016
Label2
Language
Class: Second Year
Lecturer: Hussein Mohammed Salman
Fontsize
name
Alignment
Caption
Font
Fontsize
forecolor
12
lblResult
2-center
Times New Roman
12
red
3rd Stage: Write the Codes
The codes in the cmdTest (Test) :
Dim ch As Variant, stat As String
ch = txtChar.Text
Select Case ch
Case 0 To 9
stat = "The symbol is Digit"
Case "a" To "z", "A" To "Z"
stat = "The symbol is Letter"
Case "!", "@", "#", "$", "&"
stat = "Special Symbols"
Case Else
stat = "Unknown"
End Select
lblResult.Caption = stat
The form after running will be:
This Lecture allowed on: www.uobabylon.iq.edu
2
Ministry of Higher Education And
Scientific Research
Babylon University
College of Material Engineering
Subject: Programming in Visual Basic
Polymers Engineering Department
2015-2016
Language
Class: Second Year
Lecturer: Hussein Mohammed Salman
Example2: we will solve same problem in the previous example by using
statement as below :
IF-
Dim ch As Variant, stat As String
ch = txtChar.Text
If ch >= 0 And ch <= 9 Then
stat = "The symbol is Digit"
ElseIf (ch >= "a" And ch <= "z") Or (ch >= "A" And ch <= "Z") Then
stat = "The symbol is Letter"
ElseIf ch = "!" Or ch = "@" Or ch = "#" Or ch = "$" Or ch = "&" Then
stat = "Special Symbols"
Else
stat = "Unknown"
End If
lblResult.Caption = stat
This Lecture allowed on: www.uobabylon.iq.edu
3
Ministry of Higher Education And
Scientific Research
Babylon University
College of Material Engineering
Subject: Programming in Visual Basic
Polymers Engineering Department
2015-2016
Language
Class: Second Year
Lecturer: Hussein Mohammed Salman
example2: In this example , we will design a project to check three degrees for a
student and his result average , as follow :
1st Stage: Form Design
2nd Stage: Set Properties
Object (Tool)
Command1
Command2
Text1
Text2
Text3
Label1
Property
Setting
Name
Caption
Font
Font Size
Name
Caption
Font
Font Size
Name
Text
Name
Text
Name
Text
Name
cmdCheck
Check
Times New Roman
12
cmdExit
Exit
Times New Roman
12
txtFirst
This Lecture allowed on: www.uobabylon.iq.edu
txtSecond
txtThird
lblFirst
4
Ministry of Higher Education And
Scientific Research
Babylon University
College of Material Engineering
Subject: Programming in Visual Basic
Polymers Engineering Department
2015-2016
Label2
Label3
Label4
Language
Class: Second Year
Lecturer: Hussein Mohammed Salman
Alignment
Caption
Font
Fontsize
name
Alignment
Caption
Font
Fontsize
forecolor
name
Alignment
Caption
Font
Fontsize
forecolor
name
Alignment
Caption
Font
Fontsize
forecolor
2-center
First Mark
Times New Roman
12
lblSecond
2-center
Second Mark
Times New Roman
12
Black
lblThird
2-center
Third Mark
Times New Roman
12
Black
lblAverage
2-center
Times New Roman
12
Red
3rd Stage: Write Codes :
 Command2
end
 Command1:
Dim d1 as integer,d2 as integer,d3 as integer, av as double
D1=val(txtfirst.text)
D2=val(txtSecond.text)
D3=val(txtThird.text)
If d1>=50 and d2>=50 and d3>=50 then
Av=(d1+d2+d3)/3
If av>=60 then
lblAverage.caption="The student passed and his average is "+av
else
lblAverage.caption="The student passed in the marks but not passed in his
average "
This Lecture allowed on: www.uobabylon.iq.edu
5
Ministry of Higher Education And
Scientific Research
Babylon University
College of Material Engineering
Subject: Programming in Visual Basic
Polymers Engineering Department
2015-2016
Language
Class: Second Year
Lecturer: Hussein Mohammed Salman
end if
else
lblAverage.forecolor=vbred
lblAverage.caption="The student not passed"
end if
Exercises:
1. Rewrite the code of the previous example by using select case statement?
2. Design a project to read the average of four students and print the maximum
average and minimum average of those degrees?
This Lecture allowed on: www.uobabylon.iq.edu
6
Download