Results 1 to 2 of 2

Thread: Text Box Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    5

    Text Box Question

    Hey, i need a text box to only highlight if it is clicked, or if enter is pressed, and then deselect when enter has been press again or the mouse clicks anywhere but the textbox thanks!

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Text Box Question

    Is this what you are looking for?
    Code:
    Private Sub Text1_GotFocus()
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
    End Sub
    Private Sub Text1_LostFocus()
        Text1.SelLength = 0
    End Sub
    As far as Enter key goes, I'll leave that to you. You'll want to use its KeyPress event or KeyDown event.
    In either case, check the textbox's .SelLength and if > 0 then stuff is highlighted.

    Notes:
    1. When a textbox loses focus, you can't see any selection unless the HideSelection property is set to False.
    2. When text is highlighted and you press a key under normal conditions, what was highlighted is lost and replaced with that key stroke. Ensure you address this when handling the Enter key by setting to zero, the KeyAscii or KeyCode parameter (depending on which event you'll trap the enter key in). Enter key doesn't erase text in single-line textbox mode.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

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



Click Here to Expand Forum to Full Width