vb.net 获得焦点时选择文本框的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3201029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:56:11  来源:igfitidea点击:

Select the content of textbox when it receives focus

vb.netselecttextboxfocus

提问by KoolKabin

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

我在Make a WinForms TextBox 的行为类似于浏览器的地址栏中发现了一个类似的问题

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?

所以想知道怎么做?

回答by Tim Murphy

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

以下代码继承自 TextBox 并实现您在使 WinForms TextBox 表现得像浏览器地址栏中提到的代码。

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.

将 MyTextBox 类添加到项目后,您可以对 System.Windows.Forms.Text 进行全局搜索并替换为 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

回答by Hans Olsson

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.

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

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

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

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

回答by Alejandro Amaral

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

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