Vb.net 在所有文本框上自动选择all,在gotfocus上自动选择numericUpDown控件
我有这样的代码,可以使文本框中的所有文本在got_focus上被选中:Vb.net 在所有文本框上自动选择all,在gotfocus上自动选择numericUpDown控件,vb.net,winforms,textbox,Vb.net,Winforms,Textbox,我有这样的代码,可以使文本框中的所有文本在got_focus上被选中: Private Sub myText_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles myText.GotFocus myText.SelectAll() End Sub 在VB.NET中,是否有一种方法可以让所有文本框和NumericUpDown控件在_GotFocus或_Enter上选择他的文本,而不显式地为每个控件设置
Private Sub myText_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles myText.GotFocus
myText.SelectAll()
End Sub
在VB.NET中,是否有一种方法可以让所有文本框和NumericUpDown控件在_GotFocus或_Enter上选择他的文本,而不显式地为每个控件设置这样的行为,无论该控件如何获得焦点(键盘、鼠标或编程)?是的,有并且非常简单
Private Sub TextBox2_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox2.GotFocus
TextBox2.Select(0, TextBox2.Text.Length)
End Sub
公共类MyTextBox
继承System.Windows.Forms.TextBox
Private\u作为布尔值
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 ClassAbZy,区别在于我需要在两种或两种以上不同类型的控件中选择文本,这些控件通常用于数据输入。@user973238您仍然可以对每个控件类型进行子类化并实现此功能。现在我明白了,谢谢。但现在的问题是如何用MyTextbox替换表单上的所有文本框,因为我有大约50个文本框带有特定代码?你知道有什么诀窍吗?@user973238使用
在文件中查找快速替换
将系统.Windows.Forms.TextBox
替换为我的文本框