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

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

问题描述

我有一个简单的用户窗体,其中只有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

无论您单击文本框还是将其标签到其中,它都会做您想要的.要取消选择文本,请使用箭头键.

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天全站免登陆