Highlight Text

netpicker9

Member
Joined
Dec 8, 2004
Messages
17
Programming Experience
1-3
Hi,

I need code which Highlights Text in TextBox, or in any other controls once user places Mouse in it..

Thanks
Jen
 
It only works for controls in which selecting text is possible. You can't select text in a PictureBox, a Label, a ListBox etc. so those controls don't have those members. If you want this to happen for all the controls that support it then you can simply create a single method to handle the Enter event for all those controls.
 
Create a sinlge event handler for all TextBoxes. Select all the TextBoxes in the designer. Open the Properties window and press the Events button at the top. Double-click the desired event and a single method will be created with the specified event for all TextBoxes included in the Handles clause. You then get a reference to the TextBox that raised the event via the 'sender' argument, e.g.
VB.NET:
Private Sub TextBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.Enter, _
                                                                                              TextBox2.Enter, _
                                                                                              TextBox1.Enter
    DirectCast(sender, TextBox).SelectAll()
End Sub
 
Hi

HI,

Thanks for reply, i forgot to mention one thing here..

in my form Textboxes will create at runtime, based on number of records in DB.

thanks
 
In that case you still write the same event handler at design time but just without the Handles clause. At run time, when you create each TextBox, you use the AddHandler statement to attach the method to the event, e.g.
VB.NET:
Dim tb As TextBox

For i As Integer = 1 To recordCount Step 1
    tb = New TextBox

    'Set tb properties here.

    AddHandler tb.Enter, AddressOf TextBox_Enter
    Me.Controls.Add(tb)
Next i
 
Back
Top