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.

Add Text Boxes to Form

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

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

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

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

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

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

  • 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

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

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

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

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

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

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

Tips & Warnings

  • This example adds the "Click" and "Enter" events to a single text box. To duplicate this logic for other text boxes, create "Click" and "Enter" events for them as shown in this example. Paste the same code into their event methods that you pasted into the one described here.

Related Searches


  • Photo Credit BananaStock/BananaStock/Getty Images
Promoted By Zergnet


You May Also Like

  • Text to Speech in Vb Net

    Coding text to speech applications in Microsoft Visual Basic.NET (VB.NET) can make your Windows applications more user-friendly. A useful text-to-speech application is...

  • How to Check if a Textbox Is Empty in VB

    Visual Basic is an event-driven programming language and integrated development environment developed and deployed by Microsoft Corporation. Visual Basic originated from BASIC....

  • How to Change the TextBox Border Color in Visual Basic

    Microsoft's Visual Basic allows user input via the TextBox control, and the various properties of the control allow the programmer to make...

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!