激活后如何选择文本框的内容?

Zyg*_*ygD 14 events controls vba textbox excel-vba

我有这个简单的Userform,我只有TextBox1TextBox2.我在两者中都输入了一些文字.假设焦点在(光标在)上TextBox2.当我点击时TextBox1,我希望突出显示(选中)此控件中的整个文本.因此我使用此代码:

Private Sub TextBox1_Enter()
    With TextBox1
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
    MsgBox "enter event was fired"
End Sub
Run Code Online (Sandbox Code Playgroud)

有一个MsgBox在其上装载结束,这意味着该事件的作品.但是,文本未突出显示.如何解决这个问题?

我使用该Enter事件并且不想使用该MouseDown事件,因为我需要代码在以TextBox1编程方式激活时也能工作,因此我认为该Enter事件是最佳选择,因为它在两种情况下都被触发了!该MouseDown事件的另一个缺点是:当我第二次点击时TextBox1,我不希望整个文本再次突出显示,因为焦点是在第一次点击时设置的,并且在我点击同一个控件后它没有被更改第二次; 所以在这种情况下我希望光标正常工作(不要保持文本标记).

更新
当我点击一次时TextBox1,我希望得到这个结果: 在此输入图像描述
如果再次单击,将删除突出显示,并将光标放在单击它的位置.

Sid*_*out 24

不能比这更简单我猜...

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
Run Code Online (Sandbox Code Playgroud)

无论您是单击文本框还是选中它,都可以执行您想要的操作.要取消选择文本,请使用箭头键.

说明

如果您调试代码,您将会看到,即使您已经说过.SetFocus,重点也不在于文本框..SetFocus不起作用TextBox1_Enter(),你需要专注于其余的代码工作.因此,我的另类......

替代

您可能也喜欢这个版本:)这克服了在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
Run Code Online (Sandbox Code Playgroud)


vac*_*cip 5

Pff,花了我一会儿.实际上,您的代码有效,但它会在Click事件发生之前突出显示文本.因此,您在框中单击会立即覆盖代码创建的选择.我使用了延迟选择,它有效,虽然有点恶心......

文本框的代码:

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
Run Code Online (Sandbox Code Playgroud)

请注意,即使没有{+ TimeValue("00:00:01")}部分它也可以工作,但理论上它可能会阻止它有时工作.嗯,再想一想,就把它留下吧.我怀疑它会不会引起问题.

现在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
Run Code Online (Sandbox Code Playgroud)

希望这也适合你.无趣的问题.:)干杯!


Tho*_*s G 5

我无法在 Enter 事件中选择/突出显示文本,因为随后发生的 mousedown 和 mouseup 事件在某种程度上重置了选择。

我认为实现你想要的最正确的方法是:

' 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
Run Code Online (Sandbox Code Playgroud)

因此,每次您想要以编程方式激活其中一个文本框时,您都应该调用子 SelectTboxText,在我看来,这并不是很烦人。我为此制作了 2 个按钮作为示例。