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 3: User Interface Design: Visual Basic Controls and Events
Last change: February 13 2016 18:47:26.
<<PreviousNext>>

Coding the Event Procedures

With the visual interface in place, you can now focus on the action aspect of the project. You need to consider in which events to place what code. This, of course, depends on what you would like the program to do. Consider one VB control (data field) at a time.

Date of Order

Your main concern here is the accuracy of the date entered by the user. One possibility is for the computer to fill the date into the control automatically. For simplicity, you will not do it this way now. Notice that the masked text box with the proper mask in place will automatically check to ensure that all keys are numeric. Numeric data, however, do not guarantee that the entire field is a valid date. At some point, you will need to check that the masked text box has a valid date.
Which event should you use? You can decide whether the masked text box has a valid date only when the field is complete, that is, when the user leaves the field. As explained previously, VB.NET provides an event purely for the field level data validation, the Validating event. You will place the code in this event. There is a built-in function, IsDate, that checks exactly whether a string is a valid date. If the string is a valid date, the function returns a True value; otherwise, False. (Built-in functions are discussed in more detail in Chapter 4.) You can use this function to perform the test. The code appears as follows:

Private Sub mskDate_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles mskDate.Validating If IsDate(mskDate.Text) Then ' the control has a valid date; do nothing Else ' the control has an invalid date; display a message MsgBox("Please enter a valid date.") ' Reset the focus to this control e.Cancel = True End If End Sub

In the code, when an invalid date is found in the masked text box, a message is displayed. The event also includes a parameter, Cancel. When this parameter is set to True, the focus will be reset to the current control; otherwise, the focus will move to the next control. Additional issues concerning data validation are discussed in Chapter 10.

Distributor Number

As explained previously, you need to add the list of distributors to the combo box by code. For simplicity, you will assume that these distributor numbers are 100 and 101. You also know that the Items.Add method can be used. The question is where the code should be placed. Recall that these numbers should already be in the combo box as soon as the program starts, suggesting that form load is the event to place the code. The code should appear as follows:

Private Sub frmSalesOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Add distributor numbers to cboDistributor In real applications, numbers are read from a file or database cboDistributor.Items.Add(100) cboDistributor.Items.Add(101) End Sub

Model Number

Similar to the distributor numbers, model numbers should be added to the combo box as soon as the program starts. The Form Load procedure presented previously should be modified as follows:

Private Sub frmSalesOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Add distributor numbers to cboDistributor ' In real applications, numbers are read from a file or database cboDistributor.Items.Add(100) cboDistributor.Items.Add(101) ' Add model numbers to cboModel ' In real applications, models are read from a file or database cboModel.Items.Add("Puri-Clear") cboModel.Items.Add("Environ-Safe") End Sub

In addition to getting model numbers into the combo box ready for selection, assume also that the Puri-Clear model is the most commonly ordered model. To save the user from having to make the selection, you would like to set the model as the user’s default choice whenever the user moves to this field. Note that this model is the first item in the list; therefore, you can set the combo box’s SelectedIndex to 0 to effect the choice. But where should you place the code? Again, recall that you would like for this to happen as soon as the user moves to this field; that is, when the control gets the focus. The event to use is this control’s Enter event. The code appears as follows:

Private Sub cboModel_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboModel.Enter ' set the first item as the choice cboModel.SelectedIndex = 0 End Sub

Quantity Ordered

This is a text box. You would like to ensure that the user enters only numeric keys. In what event should the code be placed? As explained, as soon as the user presses a key, the KeyPress event is triggered. The proper event to place the code is the KeyPress event of this control, txtQuantity. For simplicity, the code is left out. But a note to add the routine is put in its place. You will learn more about error checking in Chapter 10.

Private Sub txtQuantity_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtQuantity.KeyPress ' Place key error checking routine here End Sub

Special Delivery Instructions

When the user clicks the check box for “Rush Order?”, the check box is checked or unchecked. The ensuing selection of type of delivery (U.P.S. or special carrier) makes sense only when the check box is checked. The user should not be allowed to make any selection between the two radio buttons when the check box is unchecked; that is, you should enable or disable the two radio buttons when the check box is checked or unchecked. As implied in the opening statement, the code should be placed in the check box’s CheckedChanged event:

Private Sub chkRushOrder_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkRushOrder.CheckedChanged rdbUPS.Enabled = chkRushOrder.Checked rdbSpecialCarrier.Enabled = chkRushOrder.Checked End Sub

Both radio buttons’ Enabled properties are assigned the value of the check box’s Checked property. When this property is Checked (Unchecked), its value will be True (False). The radio buttons’ Enabled properties will take this value and enable (disable) themselves accordingly.
If you test the program at this point, you should notice that when the program starts, the check box is unchecked (a good thing because most orders are not rush orders). The two radio buttons, however, are also enabled. This is not a good state because you want to disable them when the rush order check box is unchecked. Go back and set (at design time) the Enabled properties of both radio buttons to False.
Tip
Always disable the controls for input purposes that do not expect any user input or action in the context. This saves the user’s time and eliminates the confusion by the user. Be sure to enable these controls again when the context requires the user input.

Buttons

Recall that it is customary to place code in the Click event to handle all expected activities when the user clicks a button. For simplicity, the code to save the screen is replaced with a note and a line of code to display the message, ”Data saved.”

Private Sub btnSave_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click ' Place the routine to save data here ' then display the following message MsgBox("Data saved.") End Sub 

The code to terminate the program has been presented in this chapter as well as the preceding chapter. It is left for you to complete.

Last change: February 13 2016 18:47:28.
<<PreviousNext>>