Results 1 to 17 of 17

Thread: [RESOLVED] Selecting Text in a Standard Textbox - Cursor blinking problem

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Resolved [RESOLVED] Selecting Text in a Standard Textbox - Cursor blinking problem

    Hi friends,

    When we want to select text in textbox programatically (like autocomplete etc.), the cursor blinks at the end of selection. How can we make it blink at the beginning of selection.
    I have seen this working in most Ms apps and when we select it using the keyboard or mouse it works, but I need to do it programatically.

    Thanks,

    Pradeep
    Attached Images Attached Images  
    Last edited by Pradeep1210; Nov 6th, 2005 at 11:45 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    Without seeing your code I can't say why this doesn't work. If I highlight text in a TextBox the cursor blinks at the begining of the highlighted text.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  3. #3

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    How do we make it blink at start of selection?
    VB Code:
    1. Private Sub Command1_Click()
    2.     Text1.Text = "Hello World"
    3.     Text1.SelStart = 4
    4.     Text1.SelLength = Len(Text1)
    5.     Text1.SetFocus
    6. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    The code in above post will select the required text but the cursor will blink at the end of selected text. I need to make it blink at the beginning of selected text.

    When you select text using mouse from right to left (end to beginning), you will get the effect I require.



    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    Here
    VB Code:
    1. Option Explicit
    2. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    3.     ByVal hwnd As Long, _
    4.     ByVal wMsg As Long, _
    5.     ByVal wParam As Long, _
    6.     lParam As Any _
    7. ) As Long
    8.  
    9. Private Const EM_SETSEL = &HB1
    10.  
    11. Private Sub Command1_Click()
    12.     Dim StartPos As Long
    13.     Dim EndPos As Long
    14.     Text1.Text = "Hello World"
    15.     StartPos = InStr(Text1.Text, "World") - 1
    16.     EndPos = Len(Text1.Text)
    17.     SendMessage Text1.hwnd, EM_SETSEL, EndPos, ByVal StartPos
    18.     Text1.SetFocus
    19. End Sub

  6. #6

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    Quote Originally Posted by moeur
    Here
    VB Code:
    1. Option Explicit
    2. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    3.     ByVal hwnd As Long, _
    4.     ByVal wMsg As Long, _
    5.     ByVal wParam As Long, _
    6.     lParam As Any _
    7. ) As Long
    8.  
    9. Private Const EM_SETSEL = &HB1
    10.  
    11. Private Sub Command1_Click()
    12.     Dim StartPos As Long
    13.     Dim EndPos As Long
    14.     Text1.Text = "Hello World"
    15.     StartPos = InStr(Text1.Text, "World") - 1
    16.     EndPos = Len(Text1.Text)
    17.     SendMessage Text1.hwnd, EM_SETSEL, EndPos, ByVal StartPos
    18.     Text1.SetFocus
    19. End Sub

    This is also doing the same thing.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    Make your textbox multiline and it will do what you want

  8. #8

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    Yes it works perfectly

    Is it somehow possible for a non-multiline textbox? I don't want the user to have multiline text in the box.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    Just add this code to prevent user from entering multilines
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     If KeyAscii = Asc(vbCr) Then KeyAscii = 0
    3. End Sub

  10. #10

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    Quote Originally Posted by moeur
    Just add this code to prevent user from entering multilines
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     If KeyAscii = Asc(vbCr) Then KeyAscii = 0
    3. End Sub
    The Enter mark is not a big problem, the problem is that if the sentence goes long, it will wrap to next line which is not visible. With non-multilne textboxes, it remains in the same line and the workaround is quite clumsy.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    Quote Originally Posted by Pradeep1210
    The Enter mark is not a big problem, the problem is that if the sentence goes long, it will wrap to next line which is not visible. With non-multilne textboxes, it remains in the same line and the workaround is quite clumsy.

    Pradeep
    well, if multiline is a problem, in that case you can use the scroll bars of the text box to avoid that problem. i tried using the above code and the scroll bar thing:
    VB Code:
    1. Option Explicit
    2. Private Declare Function ShowScrollBar Lib "user32" (ByVal hwnd As Long, ByVal wBar As Long, ByVal bShow As Long) As Long
    3. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    4.     ByVal hwnd As Long, _
    5.     ByVal wMsg As Long, _
    6.     ByVal wParam As Long, _
    7.     lParam As Any _
    8. ) As Long
    9. Private Const SB_HORZ = 0
    10. Private Const EM_SETSEL = &HB1
    11.  
    12. Private Sub Command1_Click()
    13.     Dim StartPos As Long
    14.     Dim EndPos As Long
    15.     Text1.Text = "Helloooooooooooooo Worlddddddddddddddddddddddddd"
    16.  
    17.     StartPos = InStr(Text1.Text, "World") - 1
    18.     EndPos = Len(Text1.Text)
    19.     SendMessage Text1.hwnd, EM_SETSEL, EndPos, ByVal StartPos
    20.     Text1.SetFocus
    21. End Sub
    22.  
    23. Private Sub Form_Load()
    24. ShowScrollBar Text1.hwnd, SB_HORZ, 0
    25. End Sub
    26.  
    27. Private Sub Text1_Change()
    28.     If Me.TextWidth(Text1.Text) > Text1.Width Then
    29.     ShowScrollBar Text1.hwnd, SB_HORZ, 1&
    30.     Else
    31.     ShowScrollBar Text1.hwnd, SB_HORZ, 0&
    32.     End If
    33. End Sub
    hope this helps you.
    Show Appreciation. Rate Posts.

  12. #12
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    oh i forgot, make the textbox multiline and in scroll bar properties, make it horizontal.
    Show Appreciation. Rate Posts.

  13. #13
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    Ah yes Gupta,

    Adding a scroll bar turns off word wrap and you can always hide the scroll bar if you don't want it.

  14. #14

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    The scroll bars make the situation worse. Actually I want to simulate an autocomplete combobox. This I am doing by adding a TextBox and Listbox. So as soon as the scrollbars appear or the textwraps, it seems awckward.
    At present all my code works perfect except the cursor blinking position when it is not multiline. So I need to make it better than that.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  15. #15
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    What is wrong with this?
    VB Code:
    1. Option Explicit
    2. 'Set TExt1.Multiline = true
    3. 'Set TExt.ScrollBArs = 1- Horizontal
    4. Private Declare Function ShowScrollBar Lib "user32" (ByVal hwnd As Long, ByVal wBar As Long, ByVal bShow As Long) As Long
    5. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    6.     ByVal hwnd As Long, _
    7.     ByVal wMsg As Long, _
    8.     ByVal wParam As Long, _
    9.     lParam As Any _
    10. ) As Long
    11. Private Const SB_HORZ = 0
    12. Private Const EM_SETSEL = &HB1
    13.  
    14. Private Sub Command1_Click()
    15.     Dim StartPos As Long
    16.     Dim EndPos As Long
    17.  
    18.     StartPos = InStr(Text1.Text, "World") - 1
    19.     EndPos = Len(Text1.Text)
    20.     SendMessage Text1.hwnd, EM_SETSEL, EndPos, ByVal StartPos
    21.     Text1.SetFocus
    22. End Sub
    23.  
    24. Private Sub Form_Load()
    25.     ShowScrollBar Text1.hwnd, SB_HORZ, 0
    26.     Text1.Text = "Helloooooooooooooo Worlddddddddddddddddddddddddd"
    27. End Sub
    28.  
    29. Private Sub Text1_KeyPress(KeyAscii As Integer)
    30.     If KeyAscii = Asc(vbCr) Then KeyAscii = 0
    31. End Sub

  16. #16

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    It wraps to the second line while my textbox height is equivalent to the height of a combobox (i.e. just one line).
    OR it will show a horizontal scrollbar which doesn't appear natural.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  17. #17

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Selecting Text in a Standard Textbox - Cursor blinking problem

    Yes! It's working

    Thanks,


    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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