【问题标题】:Select the content of textbox when it receives focus获得焦点时选择文本框的内容
【发布时间】:2011-03-13 04:14:16
【问题描述】:

我在Making a WinForms TextBox behave like your browser's address bar发现了一个与我类似的问题

现在我正在尝试通过使其通用来对其进行修改或使其更加不同。我想对表单中的所有文本框应用相同的操作,而无需为每个文本框编写代码……我不知道有多少。一旦我在表单中添加了一个文本框,它就应该具有类似的选择动作。

那么想知道怎么做?

【问题讨论】:

  • Windows 窗体?网络表格?什么?

标签: vb.net select textbox focus


【解决方案1】:

以下代码继承自 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

【讨论】:

    【解决方案2】:

    假设您要使用链接到的问题中已接受的解决方案,您需要做的就是每当您创建一个新文本框时,您使用 AddHandler 将相同的 3 个事件处理程序添加到每个新文本框.

    然后您需要更改事件处理程序,而不是将文本框引用为 this.textBox1,而是将其引用为 CType(sender, TextBox),这意味着他们将使用生成事件的文本框。

    编辑:我也会在这里添加那行代码,因为这样更容易阅读

    Private Sub TextBox_GotFocus (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
    

    【讨论】:

    • 嗯...很有趣...我试过了...它甚至可以工作...但是我们可以在表单上做点什么,这样我们就不需要向它们添加事件处理程序
    • @KoolKabin:我以为你的意思是动态添加文本框?否则,如果它们是在设计器中创建的,您可以将它们全部链接到同一个事件处理程序,如下所示:Private Sub TextBox_GotFocus (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
    • 谢谢它的工作。我们可以在表单级事件上再探索一步吗
    • @KoolKabin:你的意思是你想为多个表单重用同一个事件?我认为由于大多数表单事件是由“他们自己”而不是由所有者处理的(例如处理按钮单击事件的表单),我不确定它是否以同样的方式有意义。但是你当然可以做一些事情,比如从同一个基础继承两个表单并让基础处理事件,尽管它可能会有点混乱。
    【解决方案3】:

    我们使用这个自定义文本框控件:

    Public Class TextBoxX
        Inherits System.Windows.Forms.TextBox
    
        Private Sub TextBoxX_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
            SelectAll()
        End Sub
    end class
    

    您可以在 GitHub https://github.com/logico-dev/TextBoxX 上查看我们的 TextBox 的完整项目(在类固醇上)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-31
      • 2023-04-04
      • 2010-10-03
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      • 2012-02-04
      • 2011-02-08
      相关资源
      最近更新 更多