How to Highlight Text in a Text Box in VB.NET

How to Highlight Text in a Text Box in VB.NET

Visual cues in a software application can help make a user's experience more enjoyable and productive. If you create VB.NET programs, you probably work with text box controls. Text boxes allow users to communicate with your application. One way to help users identify the active text box is to highlight the text within it. VB.NET does not do this automatically, but by attaching a couple of event handlers to your code, you can make your program highlight text in text boxes as users select them.

Instructions

Add Text Boxes to Form

    1

    Launch Visual Studio and open one of your VB.NET projects.

    2

    Locate your project's start-up form in the "Solution Explorer" window. Double-click that form to display it in the design editor.

    3

    Click the "View" button at the top of Visual Studio, and then click "ToolBox." The toolbox opens.

    4

    Drag two "Textbox" controls from the toolbox onto the form. Drag the text boxes so that one appears on top of the other.

    5

    Right-click the top text box, and then click "View Properties" to display the "Properties Window." The name of the selected form appears at the top of that window. Remember the text box's name. It will probably be "TextBox1" if no other text boxes exist on your form except the two you added.

    6

    Cick the "Events" button at the top of the "Properties" window to display a list of event types. Double-click the "Enter" event. The code window opens and positions the cursor inside the method that runs the "Enter" event. VB.NET triggers this event whenever a cursor enters the text box.

    7

    Paste this code into that method:

    TextBox1.SelectionStart = 0

    TextBox1.SelectionLength = TextBox1.Text.Length

    If "TextBox1" one is not the name of the text box, replace "TextBox1" with the text box's name that you remembered. This code selects the text in the text box and highlights it when you tab to the control.

Add "OnClick" Logic

    8

    Press "F7" to display the form in design view again. Click the same text box you clicked previously, and then double-click the "Click" event in the "Properties" window. The code for the "Click" event opens.

    9

    Paste the following text into that method:

    TextBox1.SelectionStart = 0

    TextBox1.SelectionLength = TextBox1.Text.Length

    This is the same code you pasted into the "Enter" event. By placing it in this "Click" event as well, you enable VB.NET to highlight the text when users click the text box instead of tabbing to it.

    10

    Press F5 to run the project. The form opens and displays the two text boxes.

    11

    Type anything into the top text box. Press your "Tab" key to tab to the bottom text box.

    12

    Press "Tab" again to return to the top text box. VB.NET highlights the text.

    13

    Click the bottom text box. The top text box loses its highlight. Click the top text box again, and the highlight reappears.

Blog Archive