Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 5: Decision
Last change: February 13 2016 18:47:58.
<<PreviousNext>>

The If…Then…Else…End If Block

Although the previous code gives the correct result, you probably feel weird about it. Apparently, it is not completely efficient because it always disables the combo boxes even when they will be enabled immediately inside the If block. Is there an If block structure that allows the execution of a group of statements when a condition is True, and another group when the condition is False? Yes, there is—it was covered in Chapter 2. The syntax structure appears as follows:

If Condition Then Statements to be executed when condition is True Else Statements to be executed when condition is False End If

With this structure, we can code the preceding example as follows:

Private Sub chkSort_CheckChanged(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles chkSort.CheckedChanged If chkSort.Checked = vbChecked Then cboSortField1.Enabled = True cboSortField2.Enabled = True Else cboSortField1.Enabled = False cboSortField2.Enabled = False End If End Sub

This code will not only produce correct results but literally do what you expect it to: Enable the combo boxes only when the check box is clicked on and disable them only when the check box is clicked off.

Tip
Beware of the code structure. Some VB beginner may fail to see the syntax error in the following structure:
If A > B Then Console.WriteLine(A) Else Console.WriteLine(B) End If
When an If statement ends with an executable statement (as in the first line), VB considers the If block complete; therefore, the Else and End If statements below will have no context. The correct structure should be:
If A > B Then Console.WriteLine(A) Else Console.WriteLine(B) End If

Leaving One Side Out

Note that with this syntax structure, it is okay to omit statements from either the True or False side of the block. For example, the following code should cause no syntax problem:

Private Sub chkCitizen_CheckedChanged(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles chkCitizen.CheckedChanged If chkCitizen.Checked Then ‘Respondent is a citizen. No problem. Else ‘Respondent is not a citizen. Reject the service. MsgBox(“You are not qualified to serve as a Juror.”) End If End Sub

This event procedure is triggered when the user clicks the check box that asks whether the respondent is a citizen. As you can imagine, the check box is used in a data entry screen for the jury selection questionnaire. If the box is checked off, the qualification message is displayed. In this example, a statement is provided only on the False side of the block. You may wonder why not directly test whether the check box is unchecked as follows:

If chkCitizen.Checked = False Then ‘Respondent is not a citizen. Reject the service. MsgBox(“You are not qualified to serve as a Juror.”) End If

Both blocks of preceding code should work properly. The second block appears to be more concise; however, some companies have coding standards that require all conditions in the If statement be expressed in affirmation. They maintain that such a coding standard makes the code easier to review because of code logic uniformity.
It is also perfectly acceptable to provide code for only the True side of the If block as illustrated in the following code:

Private Sub txtName_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtName.Validating If Len(Trim(txtName.Text)) = 0 Then MsgBox("Please enter name before proceeding further.") e.Cancel = True 'Reset focus on txtName Else End If End Sub

This event procedure is invoked when the user moves away from a text box named txtName. The If statement checks whether the VB control is blank. If so, an error message is displayed. In addition, the Cancel property of the event argument object e is set to True, which in effect resets the focus back on txtName. As mentioned in Chapter 3, “Some Visual Basic Controls and Events,” the Validating event is triggered when the user leaves the control and before the focus is set on the next control. The event is used to perform field-level data validation, which is discussed more thoroughly in Chapter 10, “Special Topics in Data Entry.” Note that the previous If block can actually do without the Else line. You can think of the Else statement as being there as a reminder that no statement has been coded on the False side.

Last change: February 13 2016 18:48:00.
<<PreviousNext>>