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.

Selecting (Highlighting) a Portion of the Text

Imagine that you are working with a word processor such as Word. At times, you might find a need to highlight a portion of the text so that you can perform operations such as delete, cut, and paste. Both the text box and the masked text box support these operations. Suppose in your code you need to know what text is highlighted. You can use the SelectedText property of the text box to show the text that is highlighted, and the SelectionStart and SelectionLength properties of the text box to show the position at which the highlight starts and the length of the selection. Note that the first position has a value of 0 for the SelectionStart property.

Try This
Draw a text box and a button on a new form. Name the text box txtName. Name the button btnShow and set its text to Show. Enter the following code:
Private Sub btnShow_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click MsgBox("SelectedText=" & txtName.SelectedText & _ ". SelectionStart=" & txtName.SelectionStart & ". SelectionLength=" & _ txtName.SelectionLength) End Sub
Run the project. Enter some text in the text box, and highlight a portion of the text; then click the Show button. What do you see?

You can also use the SelectionStart and SelectionLength properties in code to select (highlight) a portion of the text. For example, the following code will highlight the content of a text box named txtName when the user tabs into the control.

Private Sub txtName_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.Enter txtName.SelectionStart = 0 'Set the cursor at the beginning 'Highlight the text from the beginning txtName.SelectionLength = Len(txtName.Text) End Sub

The code is placed in the txtName’s Enter event procedure, which is triggered when the user presses the Tab key to move the focus to txtName. Its SelectionStart property is set to 0 to place the cursor at the beginning of the text. The following expression:


uses the Len function, which returns the length of the parameter enclosed in the pair of parentheses (txtName.Text). The SelectionLength property of txtName is then set to this returned value.  Because the SelectionLength property highlights the text starting from the current cursor position (which is set at the beginning by the SelectionStart property), the entire text in txtName will be highlighted. Functions are discussed further in Chapter 4.

In general, the key you enter into a text box is inserted to the existing text. This is recognized as the Insert mode. Another mode of typing is recognized as the type over mode, in which whatever you type replaces the existing text. You can make a text box perform the type over mode with a simple block of code. Recall that the key you enter replaces the highlighted text; therefore, all you have to do is highlight the next position in the text box as soon as the user enters a key. The text box has a TextChanged event, which occurs when its text changes (when your key is entered). You can use this event to do the trick:
Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged 'Ensure the cursor is placed in a proper position If txtName.SelectionStart < 0 Then txtName.SelectionStart = 0 End If 'highlight the character where the cursor is txtName.SelectionLength = 1 End Sub

The following table provides a summary of the new properties discussed in this section.

Property Applicable Object Use/Remark
Text Both controls For the user to enter data; value can be set at design time or assigned by code at run time (for example, txtName.Text = “John Smith”).
Masked text box only Entered data or value assigned will be in accordance with the mask pattern.
Mask Masked text box only To define the pattern for the expected data type; for example, a 0 will expect a number for the corresponding position in the Text property.
SelectionStart Both controls To set the cursor position in the field (control); for example, the code txtName.SelectionStart = 1 will place the cursor at the second position of the field.
SelectionLength Both controls To select a portion of the text; for example, the statement txtName.SelLength = 3 will select (highlight) three characters in the field beginning at the current cursor position.
SelectedText Both controls Returns the text string that is currently highlighted.
Last change: February 13 2016 18:47:26.