0

I got many textboxes (about 10) in a form. I want the text in the textbox to be higlighted whenever it gets foucs. The code for that looks like:

Private Sub txtBillNo_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBillNo.GotFocus
   HoverText(txtBillNo)
End Sub

Private Sub HoverText(ByRef ctrl as TextBox)
   ctrl.SelectAll()
End Sub

It works perfectly, but I though I could do some code optimization here. Since, I have about 10 textboxes (and many other forms containing several textboxes), I have to HoverText(TextBox) in every Private Sub.. Handles TextBox.GotFocus for every textbox in each form.

I look for any form event (or any other way) that is trigerred when focus is given to another control (textbox) within the form, either by MouseClick or TAB so that HoverText(TextBox) is needed to be written only once for a form.

1
  • 1
    Side note: use .SelectAll instead of .Select(). Commented Jan 13, 2013 at 10:03

2 Answers 2

3

You can list all your textboxes in the Handles clause:

Private Sub atextbox_GotFocus(ByVal sender As System.Object, ByVal e As _
 System.EventArgs) _
           Handles txt1.GotFocus, _
                   txt2.GotFocus, _
                   txt3.GotFocus, _
                   (...remaining text boxes..)
                   txt9.GotFocus, _
                   txt10.GotFocus

Or you can add handlers to all textboxes when loading the form:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each t In Me.Controls.OfType(Of TextBox)()
        AddHandler t.GotFocus, AddressOf HoverText
    Next
End Sub

Private Sub HoverText(ByVal sender As System.Object, ByVal e As System.EventArgs)
    DirectCast(sender, TextBox).SelectAll()
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

In the first option, how do I know which textbox triggered teh event. I discovered that 'sender' is the Form.
@RajBD No, sender would be the textbox in a GotFocus handler.
Oh yeah! I was going through different error. Thought that was the case. Fixed it. Thanks!
2

The .NET way to alter the behavior of a class is to derive a new one. That's well supported by VB.NET and Winforms as well. For any class derived from Control, you can intercept an event by overriding the protected OnXxx event where Xxx is the event name. You should be using the Enter event btw. Use Project + Add Class and make the code look like this:

Public Class MyTextBox
    Inherits TextBox

    Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
        Me.Select(0, Me.Text.Length)
        MyBase.OnEnter(e)
    End Sub
End Class

Compile. Go back to your form and note that you now have a new control on the top of the toolbox. Drag it on the form and note how it now behaves the way you want it, without writing any code or event handler in your form at all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.