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

前端 未结 10 2184
灰色年华
灰色年华 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:51

    I couldn't manage to select/highlight text in the Enter event as the the mousedown and mouseup events coming after are somewhat resetting the selection.

    I think the most proper way of achieving what you want is this :

    ' if you want to allow highlight more then once, reset the  variable LastEntered prior to call SelectTboxText:
    '       LastEntered = ""
    '       SelectTboxText TextBox2
    
    
    Dim LastEntered As String
    
    
    ' Button to select Textbox1
    Private Sub CommandButton1_Click()
        SelectTboxText TextBox1
    End Sub
    
    ' Button to select Textbox2
    Private Sub CommandButton2_Click()
        SelectTboxText TextBox2
    End Sub
    
    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        SelectTboxText TextBox1
    End Sub
    
    
    Private Sub TextBox2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
         SelectTboxText TextBox2
    End Sub
    
    
    Public Sub SelectTboxText(ByRef tBox As MSForms.TextBox)
    
        If LastEntered <> tBox.Name Then
    
            LastEntered = tBox.Name
    
            With tBox
                .SetFocus
                .SelStart = 0
                .SelLength = Len(.Text)
            End With
    
        End If
    
    End Sub
    

    So each time you want to activate one of the textbox programmatically, you should call the sub SelectTboxText, which is not really annoying IMO. I made 2 buttons for this as an example.

    0 讨论(0)
  • 2020-12-06 06:00

    There's another solution than the one given by Siddharth.

    EDIT: but there's this bug of SendKeys, so the solution I propose below is a lot worse than Siddharth one. I keep it in case one day the bug is corrected...

    It relies on the property EnterFieldBehavior of the TextBox field. This property works only when the Tab key is pressed to enter that field, and if this property is fmEnterFieldBehaviorSelectAll (0) the whole field text is automatically selected.

    So a dummy caret movement between fields when the form is shown, will activate the feature automatically. For instance this movement can be achieved by pressing Tab to move to the next field, and pressing Shift+Tab to move to the previous field (so back to the original field):

    Private Sub UserForm_Activate()
        SendKeys "{TAB}+{TAB}"
    End Sub
    

    The (very little) advantage of this solution is that you can tune your user form by editing manually the properties EnterFieldBehavior, TabIndex, TabKeyBehavior and TabStop without changing the VBA code anymore to set "select all" on the field with the initial focus.

    In short, the VBA code above tells to consider the property EnterFieldBehavior of the field which has the initial focus when the user form is displayed (provided that it's a TextBox or ComboBox field of course).

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

    use this

    Private Sub TextBox1_Enter()
        With TextBox2
            .ForeColor = vbBlack
            .Font.Bold = False
        End With
        With TextBox1
            .ForeColor = vbRed
            .Font.Bold = True
        End With
    End Sub
    
    Private Sub TextBox2_Enter()
        With TextBox1
            .ForeColor = vbBlack
            .Font.Bold = False
        End With
        With TextBox2
            .ForeColor = vbRed
            .Font.Bold = True
        End With
    End Sub
    
    0 讨论(0)
  • 2020-12-06 06:05

    The behavior you're trying to implement is already built in to the TextBox. When you move the mouse over the left side of the text box, the mouse pointer will point to the right. If you click, it will select all the text in the field. Clicking anywhere else will deselect the text.

    I will try a few other strategies to see if I can get this to work in one Sub.

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