0

I have such code to make all text in textbox selected on got_focus:

Private Sub myText_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles myText.GotFocus
    myText.SelectAll()
End Sub

Is here a way in VB.NET to get that all TextBoxes and NumericUpDown controls selects his text on _GotFocus or _Enter without to explicitly set such behavior for every single control and no matter how this control gets a focus (keyboard, mouse or programmatic)?

Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45
Wine Too
  • 4,515
  • 22
  • 83
  • 137
  • 1
    [Possible Duplicate](http://stackoverflow.com/questions/3201029/select-the-content-of-textbox-when-it-reveives-focus) – Abdusalam Ben Haj Feb 03 '13 at 11:16
  • AbZy, difference is in that that I need to select text in controls of two or more different types which are commonly used for data input. – Wine Too Feb 03 '13 at 11:22
  • 1
    @user973238 You could still subclass each control type and implement this functionality. – Abdusalam Ben Haj Feb 03 '13 at 11:25
  • I see now I can, thanks. But now is question how to replace all textboxes on the form with MyTextbox since I have about 50 of them with specific code? Do you know any trick for that? – Wine Too Feb 03 '13 at 11:32
  • 1
    @user973238 use the `Find In Files` `Quick Replace` to replace `System.Windows.Forms.TextBox` with `MyTextBox` – Abdusalam Ben Haj Feb 03 '13 at 11:38
  • WOW, I edited myForm.Designer.vb successfully from outside (with Geany) and everything works without error but with new, subclassed controls instead of generic teytboxes. Thank you very much! That helped me a lot. – Wine Too Feb 03 '13 at 12:02

2 Answers2

0

Yes, there is and very simple.

   Private Sub TextBox2_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox2.GotFocus
        TextBox2.Select(0, TextBox2.Text.Length)
    End Sub
spajce
  • 7,044
  • 5
  • 29
  • 44
0

Public Class MyTextBox Inherits System.Windows.Forms.TextBox Private _focused As Boolean

Protected Overrides Sub OnEnter(e As EventArgs)
    MyBase.OnEnter(e)
    If MouseButtons = MouseButtons.None Then
        SelectAll()
        _focused = True
    End If
End Sub

Protected Overrides Sub OnLeave(e As EventArgs)
    MyBase.OnLeave(e)
    _focused = False
End Sub

Protected Overrides Sub OnMouseUp(mevent As MouseEventArgs)
    MyBase.OnMouseUp(mevent)
    If Not _focused Then
        If SelectionLength = 0 Then
            SelectAll()
        End If
        _focused = True
    End If
End Sub

End Class

SNK
  • 1