当文本框获得焦点时选择文本框的内容

发布于 2024-09-08 14:03:05 字数 287 浏览 3 评论 0原文

我在 让 WinForms TextBox 表现出类似的问题就像你的浏览器的地址栏

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

所以想知道该怎么做?

I have found a similar question to mine in Making a WinForms TextBox behave like your browser's address bar

Now i am trying to modify or make it some more different by making it general. I want to apply same action to all the textboxes in form without write code for each one... how many i dun know. As soon as i add a textbox in my form it should behave with similar action of selecting.

So wondering how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新一帅帅 2024-09-15 14:03:05

以下代码继承自 TextBox 并实现您在 中提到的代码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

The following code inherits from TextBox and implements the code you mentioned in Making a WinForms TextBox behave like your browser's address bar.

Once you've added the MyTextBox class to your project you can do a global search for System.Windows.Forms.Text and replace with MyTextBox.

The advantage of using this class is you can't forget to wire all the events for every textbox. Also if you decide on another tweak for all textboxes you have one place to add the feature.

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
我爱人 2024-09-15 14:03:05

假设您要使用链接到的问题中已接受的解决方案,您所需要做的就是每当创建新文本框时,使用 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

Assuming you're going to use the accepted solution from the question you link to, all you'd need to do would be that whenever you create a new textbox, you use AddHandler to add the same 3 eventhandlers to each new textbox.

Then you need to change the event handlers to instead of referencing the textbox as this.textBox1 they'll reference it as CType(sender, TextBox) which means that they'll use the textbox that generated the event.

Edit: I'll add that line of code here as well since it's easier to read then

Private Sub TextBox_GotFocus (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
酷到爆炸 2024-09-15 14:03:05

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

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

We use this custom textbox control:

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

You can see the full project of our TextBox (on steroids) on GitHub https://github.com/logico-dev/TextBoxX

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文