Koo*_*bin 4 vb.net select textbox focus
在Make a WinForms TextBox中,我发现了类似的问题,就像浏览器的地址栏一样
现在我试图通过使它变得通用来修改或使其更加不同.我想对表单中的所有文本框应用相同的操作,而不是每个文本框都有代码...我知道多少个.只要我在表单中添加一个文本框,它就应该采用类似的选择操作.
所以想知道怎么做?
以下代码继承自TextBox,并实现了在Make a WinForms TextBox中提到的代码,就像浏览器的地址栏一样.
将MyTextBox类添加到项目后,可以对System.Windows.Forms.Text进行全局搜索,并替换为MyTextBox.
使用此类的优点是您不能忘记为每个文本框连接所有事件.此外,如果您决定对所有文本框进行另一次调整,则可以在一个位置添加该功能.
Imports System
Imports System.Windows.Forms
Public Class MyTextBox
Inherits TextBox
Private alreadyFocused As Boolean
Protected Overrides Sub OnLeave(ByVal e As EventArgs)
MyBase.OnLeave(e)
Me.alreadyFocused = False
End Sub
Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
MyBase.OnGotFocus(e)
' Select all text only if the mouse isn't down.
' This makes tabbing to the textbox give focus.
If MouseButtons = MouseButtons.None Then
Me.SelectAll()
Me.alreadyFocused = True
End If
End Sub
Protected Overrides Sub OnMouseUp(ByVal mevent As MouseEventArgs)
MyBase.OnMouseUp(mevent)
' Web browsers like Google Chrome select the text on mouse up.
' They only do it if the textbox isn't already focused,
' and if the user hasn't selected all text.
If Not Me.alreadyFocused AndAlso Me.SelectionLength = 0 Then
Me.alreadyFocused = True
Me.SelectAll()
End If
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25473 次 |
| 最近记录: |