0

我有这样的代码可以在 got_focus 上选择文本框中的所有文本:

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

在 VB.NET 中是否有一种方法可以让所有 TextBoxes 和 NumericUpDown 控件在 _GotFocus 或 _Enter 上选择他的文本,而无需为每个控件显式设置此类行为,无论该控件如何获得焦点(键盘、鼠标或编程)?

4

2 回答 2

0

公共类 MyTextBox 继承 System.Windows.Forms.TextBox Private _focused 作为布尔值

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

结束类

于 2013-03-06T19:15:54.563 回答
0

是的,有而且很简单。

   Private Sub TextBox2_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox2.GotFocus
        TextBox2.Select(0, TextBox2.Text.Length)
    End Sub
于 2013-02-03T12:53:30.503 回答