Results 1 to 11 of 11

Thread: How to turn on and off the cursor in the textbox?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    292

    How to turn on and off the cursor in the textbox?

    Hi,everyone.

    I would like to know how to turn on and off the cursor in the textbox control?

    Thanks for your help.

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: How to turn on and off the cursor in the textbox?

    VB Code:
    1. Private Declare Function ShowCaret Lib "user32" (ByVal hwnd As Long) As Long
    2. Private Declare Function HideCaret Lib "user32" (ByVal hwnd As Long) As Long
    3.  
    4. Private Sub Command1_Click()
    5.     HideCaret Text1.hwnd
    6. End Sub
    7.  
    8. Private Sub Command2_Click()
    9.     ShowCaret Text1.hwnd
    10. End Sub

  3. #3
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: How to turn on and off the cursor in the textbox?

    why do you want to turn on/off the cursor? what you would like to do?
    CS

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to turn on and off the cursor in the textbox?

    Even though Bushmobile showed the correct code it is placed in the wrong place. You can't really place the HideCaret call in the Click event of a command button (well you can but it wont work as expected). The reason is that only one control can have a text caret at one particular time, and when you click a button the textbox has lost focus and the caret has already been destroyed. It will however be recreated when the textbox regains focus again, so you should call HideCaret in the GotFocus event of the textbox instead.
    VB Code:
    1. Private Sub Text1_GotFocus()
    2.     HideCaret Text1.hwnd
    3. End Sub

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: How to turn on and off the cursor in the textbox?

    Good point JA, I wrote the example before engaging my brain.

  6. #6
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: How to turn on and off the cursor in the textbox?

    Hey guys,

    Could you tell me what is the use of hiding and showing the cursor in text box?
    CS

  7. #7
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: How to turn on and off the cursor in the textbox?

    well... you might want to just show text in the textbox, rather than allow the user to enter text, i.e. it behaves like a label, but you have all the extra functionality and ability to modify it because it has an hWnd.

  8. #8
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: How to turn on and off the cursor in the textbox?

    To restrict the user to enter something in text box we can use the locked property right.
    CS

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: How to turn on and off the cursor in the textbox?

    but the caret is still displayed, so you need to hide it.

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to turn on and off the cursor in the textbox?

    Hiding the caret doesn't restrict the user from entering text by itself so the Locked property must also be set. But as Bushmobile said there are times when you might want the textbox to act as a scrollable label but you don't want the annoying flashing caret to appear. The caret is pretty useless if you don't allow the user to enter text.

  11. #11
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: How to turn on and off the cursor in the textbox?

    Ok thanks. Now I understood everything.
    CS

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