Visual Basic program application usually use If..Then..Else..End If statement. In this posting I would like to write a sample of simple program in VB6 which use If..Then..Else..End If statement with 2 conditions or more.
The syntax of the statement is the following:
If Condition-1 Then
Statement-1
Else
Statement-2
End If
If Condition-1 Then
Statement-1
Else
If Condition-2 Then
Statement-2
Else
If Condition-3 Then
Statement-3
Else
Statement-4
End If
End If
End If
Note: If the conditions are too much it is not effective if using If..Then..Else..End If statement, however it is better using Select Case..End Select statement.
The scenario of the program is as below:
Your program coding will look like the folling list:
Private Sub cmdRegister_Click()
'recheck data validity
If txtName.Text = "" Then
MsgBox "You must fill in the Name first"
Exit Sub
End If
If txtGender.Text = "" Then
MsgBox "You must fill in the Gender code"
Exit Sub
End If
If txtGender.Text <> "M" And txtGender.Text <> "F" Then
MsgBox "Just 'M' or 'F' for the Gender code "
Exit Sub
End If
'display greeting
lblGreeting.Caption = "Welcome " & IIf(txtGender.Text = "M", "Mr. ", "Mrs. ") & _
Trim(txtName.Text) & ". Thank you for joining our club."
End Sub
Private Sub Form_Load()
lblGender.Caption = ""
lblGreeting.Caption = ""
End Sub
Private Sub txtGender_Change()
If txtGender.Text <> "M" And txtGender.Text <> "F" Then
MsgBox "Invalid value, you should fill in 'M' or 'F'"
Else
If txtGender.Text = "M" Then
lblGender.Caption = "Male"
Else
lblGender.Caption = "Female"
End If
End If
If txtName.Text <> "" Then
cmdRegister.Enabled = True
Else
cmdRegister.Enabled = False
End If
End Sub
Private Sub txtName_Change()
If txtName.Text = "" Then
cmdRegister.Enabled = False
Else
If txtGender.Text = "" Then
cmdRegister.Enabled = False
Else
cmdRegister.Enabled = True
End If
End If
End Sub
Please look at below screen shot as your illustration:
0 comments:
Post a Comment