How to select the contents of a textbox once it is activated?

前端 未结 10 2122
灰色年华
灰色年华 2020-12-06 05:16

I have this simple Userform, where I only have TextBox1 and TextBox2. I enter some text in both of them. Assume the focus is on (the cursor is in)

相关标签:
10条回答
  • 2020-12-06 05:39

    I know this is well out of date but I'm leaving this here in case it helps someone in my position.

    What I want is:

    • If I click on the box for the first time: select all the text
    • If I click on it a subsequent time: put the cursor where the mouse is and allow me to use the mouse to select a substring

    Firstly it is important to know that "Select all the text" is the default behaviour when tabbing into a TextBox and that "Put the cursor here" is the default behaviour when clicking on a TextBox so we only need to worry about what the mouse is doing.

    To do this, we can keep track of the Active Control, but only while the mouse is moving over our TextBox (ie. before the Click)

    Code:

    Private m_ActiveControlName As String
    
    Private Sub Text1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        m_ActiveControlName = Me.ActiveControl.Name
    End Sub
    
    Private Sub Text1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        If m_ActiveControlName <> Me.Text1.Name Then
            Call Text1_Enter   'we don't have to use Text1_Enter for this, any method will do
            Exit Sub           'quit here so that VBA doesn't finish doing its default Click behaviour
        End If
    End Sub
    
    Private Sub Text1_Enter()
        With Text1
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
    End Sub
    
    0 讨论(0)
  • 2020-12-06 05:43

    Can't be more simple than this I guess...

    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
    ByVal X As Single, ByVal Y As Single)
        With TextBox1
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
    End Sub
    

    Whether you click on the textbox or you tab into it, it will do what you want. To deselect the text, use the arrow keys.

    Explanation

    If you debug the code you will see that even though you have said .SetFocus, the focus is not on the Textbox. .SetFocus doesn't work in TextBox1_Enter() and you need to have focus for the rest of the code to work. And hence my alternative...

    Alternative

    You may also like this version :) This overcomes the limitation of using the mouse in the TextBox

    Dim boolEnter As Boolean
    
    Private Sub TextBox1_Enter()
        boolEnter = True
    End Sub
    
    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
    ByVal X As Single, ByVal Y As Single)
        If boolEnter = True Then
            With TextBox1
                .SelStart = 0
                .SelLength = Len(.Text)
            End With
            boolEnter = False
        End If
    End Sub
    
    0 讨论(0)
  • 2020-12-06 05:43
    Private Sub UserForm_Initialize()                                                                
        TextBox1.SetFocus
        TextBox1.SelStart = 0
        TextBox1.SelLength = Len(TextBox1.Text)
    End Sub   
    

    Add this to the form's code

    0 讨论(0)
  • 2020-12-06 05:44

    Try the same code with TextBox1_MouseDown. It should work.

    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        With TextBox1
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
        MsgBox "Text in TextBox1 is selected"
    End Sub
    
    0 讨论(0)
  • 2020-12-06 05:46

    This is somewhat an enhancement of what @vacip posted. The benefit you get is that you don't need to add a separate method in the Module for each new textbox.

    The following code in your User Form:

    '===== User Form Code ========
    
    Option Explicit
    
    Private Sub TextBox1_Enter()
        OnTextBoxEnter
    End Sub
    
    Private Sub TextBox2_Enter()
       OnTextBoxEnter
    End Sub
    
    Private Sub TextBox3_Enter()
       OnTextBoxEnter
    End Sub
    

    The following code goes in a Module:

    '===== Module Code ========
    
    Sub SelectAllText()
        SendKeys "{HOME}+{END}", True
    End Sub
    
    Sub OnTextBoxEnter()
       Application.OnTime Now + 0.00001, "SelectAllText", Now + 0.00002
    End Sub
    
    0 讨论(0)
  • 2020-12-06 05:49

    Pff, took me a while. Actually, your code works, but it highlights the text BEFORE the click event happens. So you clicking in the box instantly overrides the selection created by the code. I have used a delayed selection, and it works, though it is a bit disgusting...

    The code for the textboxes:

    Private Sub TextBox1_Enter()
      Application.OnTime Now + TimeValue("00:00:01"), "module1.SelectText1"
    End Sub
    
    Private Sub TextBox2_Enter()
      Application.OnTime Now, "module1.SelectText2"
    End Sub
    

    Note that it works even withouth the {+ TimeValue("00:00:01")} part, but it might theoretically stop it from working at times. Hmm, on a second thought, just leave it out. I doubt it would ever cause a problem.

    Now the code in module1:

    Sub SelectText1()
      UserForm1.TextBox1.SelStart = 0
      UserForm1.TextBox1.SelLength = Len(UserForm1.TextBox1.Text)
    End Sub
    
    Sub SelectText2()
      UserForm1.TextBox2.SelStart = 0
      UserForm1.TextBox2.SelLength = Len(UserForm1.TextBox2.Text)
    End Sub
    

    Hope this works for you too. Ineresting problem. :) Cheers!

    0 讨论(0)
提交回复
热议问题