vb.net – 在获得焦点时选择文本框的内容
发布时间:2020-12-17 07:25:42 所属栏目:百科 来源:网络整理
导读:我在 Making a WinForms TextBox behave like your browser’s address bar找到了类似的问题 现在我试图通过使它变得通用来修改或使其更加不同.我想对表单中的所有文本框应用相同的操作,而不是每个文本框都有代码…我知道多少个.只要我在表单中添加一个文本
我在
Making a WinForms TextBox behave like your browser’s address bar找到了类似的问题
现在我试图通过使它变得通用来修改或使其更加不同.我想对表单中的所有文本框应用相同的操作,而不是每个文本框都有代码…我知道多少个.只要我在表单中添加一个文本框,它就应该采用类似的选择操作. 所以想知道怎么做? 解决方法
以下代码继承自TextBox并实现您在
Making a WinForms TextBox behave like your browser’s address bar中提到的代码.
将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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |