Results 1 to 5 of 5
  1. #1
    Star Lounger
    Join Date
    Jan 2001
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Starting With Defualt Text Highlighted in Textbox

    Am using Word 97 and would like to know how I can get default text in a textbox of my userform highlighted when the focus is on it. This is the first place the cursor goes to when my UserForm starts. However, I would like the default value to be highlighted. Your help greatly appreciated.

    Gary

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Starting With Defualt Text Highlighted in Textbox

    Hi Gary,
    There may be a simpler way but this should work:
    Private Sub UserForm_Initialize()
    With Me.txtTimeElapsed
    .SelStart = 0
    .SelLength = Len(.Text)
    End With
    End Sub
    If you've already got code in the Initialize event, then just add the code into it, otherwise just copy and paste this into the form's module.
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Starting With Defualt Text Highlighted in Textbox

    Hi Rory,

    I was just playing around with this one (more like banging my head against a brick wall).[img]/w3timages/icons/crazy.gif[/img]
    Anyway, the sample you've suggested (along with about 4 other approaches) unfortunately doesn't seem to do the trick.

    SelStart will put the cursor at the position indicated by the number.
    SelLength will return the length of the selection, that the user selects in the textbox, after the form has shown - but it won't set the selection in the Initialize event. (There's a sample procedure in the VBA help which demonstrates the interrelationship between SelStart, SelLength and SelText.)

    The ListBox control provides a direct means to select an item in the list, but the TextBox and ComboBox controls, alas, don't.

    The only workaround I could see was to use the oft-maligned SendKeys method (seems fairly safe in this case though):

    <pre>Private Sub UserForm_Initialize()
    With TextBox1
    .Text = "Default Text"
    .SetFocus
    SendKeys "+{Left 12}"
    End With
    End Sub
    </pre>


    Gary F.

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Starting With Defualt Text Highlighted in Textbox

    Hi Gary,
    That's true if you use setfocus to move the focus to that control, but if you simply set the tabindex of the textbox to 0 and don't use setfocus at all, it does seem to work. (though I admit I tested it in Excel 2000 - I presumed it would work the same in Word. Dangerous assumption I know[img]/w3timages/icons/grin.gif[/img])
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Star Lounger
    Join Date
    Jan 2001
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Starting With Defualt Text Highlighted in Textbox

    Thanks Rory. Works very well for my form. Thank you so much for your help.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •