locked
How Can I Highlight Text in a VBA Text Box So that the Text Will Vanish If I Press DEL? RRS feed

  • Question

  • The Word Advanced Find command has the convenient feature that it remembers the string that was earlier searched for. This string appears in the text box of the Advanced Find command with highlighting on it. This highlighting indicates that if you press the DEL key, the string will be erased from the text box, and then you can type a new string. That's very convenient.

    I use a form that is similar to the form for the Advanced Find Command to perform a special type of search. Everything works properly, but with one problem: If I execute the macro to display the form, the text box on the form remembers the previous string that was searched for and the text box displays the string (so that the string can be used again if desired). However, I would like the string in the text box to be highlighted, just like the string in the Advanced Find text box is highlighted. And if I press the DEL key, I would like the text to vanish, just like in the Advanced Find command. But although I've looked carefully, I can't find a way to do that.

    Of course, I don't just want highlighting in the text box, but I want the DEL functionality. That way it's easy to clear the text box to type a new string.

    Do you know how to display a form with a text box with the string in the text box highlighted in a way that DEL will delete the string?

    Thanks for your help,

    Don Macnaughton
    Monday, November 28, 2016 5:38 PM

Answers

  • Set focus to the text box and select the text in the UserForm_Initialize event procedure of the userform:

    Private Sub UserForm_Initialize()
        With Me.TextBox2 ' Substitute the name of the text box
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Value)
        End With
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Monday, November 28, 2016 8:17 PM

All replies

  • Set focus to the text box and select the text in the UserForm_Initialize event procedure of the userform:

    Private Sub UserForm_Initialize()
        With Me.TextBox2 ' Substitute the name of the text box
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Value)
        End With
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Monday, November 28, 2016 8:17 PM
  • Hans:  Thank you.  That's perfect. Don
    Monday, November 28, 2016 8:33 PM