If TypeOf Statement and IIf Function in Visual Basic
If TypeOf Statement:
If TypeOf Statement allows conditional execution based on
processing to a control Type. Syntax of If TypeOf Statement is
If TypeOf ControlName is ControlType Then
[StatementBlock
1]
[ElseIf TypeOf ControlName is ControlType Then
[StatementBlock 2]
[Else
[Statement Block
n]]
End If
Where ControlName is the valid name of control and
ControlType must also be valid one.
Exapmle:
Start Visual Basic with new Standard EXE project, place a
CommandButton and a TextBox control on a Form and write following code in the
Click event of the CommandButton.
Private Sub Command1_Click ()
If TypeOf
Text1 is Textbox Then
Msgbox (“Textbox”)
End If
End Sub
|
Press F5 key to run this project and Click the CommandButton.
The result of above code will be in the Form of MsgBox function.
IIf Function:
IIf Function is closely related with If – Then – Else – structure.
The syntax of IIf function is
Where value1 will be returned if condition is true otherwise
value2 will be returned.
Example:
Start Visual Basic with new Standard EXE project, place a
CommandButton on a Form and write following code in the Click event of the
CommandButton control.
Private Sub Command1_Click ()
Dim x As Integer
X = 6
Print IIf ( x >
5, “ Greater then 5 “ , “Less then 5 “ )
|
Press F5 key to run this project and Click the CommandButton
to check its output.
0 comments:
Post a Comment