激活后如何选择文本框的内容? [英] How to select the contents of a textbox once it is activated?

查看:19
本文介绍了激活后如何选择文本框的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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) the TextBox2. When I click on TextBox1, I want the whole text in this control to be highlighted (selected). Thus I use this code:

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

最后有一个 MsgBox 被加载,这意味着该事件有效.但是,文本未突出显示.如何解决这个问题?

There is a MsgBox at the end which is loaded, that means the event works. However, the text is not highlighted. How to fix this?

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

I use the Enter event and don't want to use the MouseDown event, because I need the code to also work when the TextBox1 is activated programatically, so I feel the Enter event to be the best choice, as it's fired in both cases! Another drawback of the MouseDown event is: when I click for the second time on the TextBox1, I would not expect the whole text to be highlighted anymore, because the focus was set on the first click and it was not changed after I clicked on the same control for the second time; so in this case I would like the cursor to act normally (not to keep the text marked).

更新
当我单击一次 TextBox1 时,我希望得到以下结果:
如果再次点击,高亮将被移除,光标将被放置在被点击的地方.

Update
When I click once on the TextBox1, I expect to have this result:
If clicked again, the highlight would be removed and the cursor would be placed in the place where it was clicked.

推荐答案

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

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

无论您是单击文本框还是按 Tab 键进入它,它都会执行您想要的操作.要取消选择文本,请使用箭头键.

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.

说明

如果您调试代码,您会看到即使您说过.SetFocus,焦点也不在文本框上..SetFocusTextBox1_Enter() 中不起作用,您需要专注于其余代码才能工作.因此我的替代方案...

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...

替代方案

您可能也喜欢这个版本:) 这克服了在文本框中使用鼠标的限制

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

这篇关于激活后如何选择文本框的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆