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
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.

As the User Leaves the Field: The Leave Event

When the user finishes working with a field (VB control), he or she leaves that field. Usually, this is the moment you want to perform additional operations on a field, such as converting all letters in the field to uppercase letters. When the user leaves a field, the Leave event is triggered. As with all previous events, you can place code in this event to perform any operations you deem desirable. For example, if you want all characters entered into a field named txtUser to be converted to the uppercase, you can place the code in the control’s Leave event as follows:

Private Sub txtUser_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUser.Leave txtUser.Text = UCase(txtUser.Text) End Sub

In the preceding code, UCase is a built-in function that converts a text string to uppercase letters. The statement instructs the computer to convert the content of txtUser to the uppercase and then assign the result to the text property of txtUser.
Prior to VB.NET, LostFocus was the event in which the field level data validation code was placed. (The LostFocus event is still available in VB.NET. Stay away from it if you can.) There are, however, potential problems with placing the data validation code in this event. For the convenience of data validation, VB.NET introduces two new events—Validating and Validated—that occur after the Leave event but before the LostFocus event. Data validation and the Validating event are discussed in Chapter 10.

Last change: February 13 2016 18:47:27.