1 Attachment(s)
[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 :)
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. ;)
Re: Selecting Text in a Standard Textbox - Cursor blinking problem
How do we make it blink at start of selection?
VB Code:
Private Sub Command1_Click()
Text1.Text = "Hello World"
Text1.SelStart = 4
Text1.SelLength = Len(Text1)
Text1.SetFocus
End Sub
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 :)
Re: Selecting Text in a Standard Textbox - Cursor blinking problem
Here
VB Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any _
) As Long
Private Const EM_SETSEL = &HB1
Private Sub Command1_Click()
Dim StartPos As Long
Dim EndPos As Long
Text1.Text = "Hello World"
StartPos = InStr(Text1.Text, "World") - 1
EndPos = Len(Text1.Text)
SendMessage Text1.hwnd, EM_SETSEL, EndPos, ByVal StartPos
Text1.SetFocus
End Sub
Re: Selecting Text in a Standard Textbox - Cursor blinking problem
Quote:
Originally Posted by moeur
Here
VB Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any _
) As Long
Private Const EM_SETSEL = &HB1
Private Sub Command1_Click()
Dim StartPos As Long
Dim EndPos As Long
Text1.Text = "Hello World"
StartPos = InStr(Text1.Text, "World") - 1
EndPos = Len(Text1.Text)
SendMessage Text1.hwnd, EM_SETSEL, EndPos, ByVal StartPos
Text1.SetFocus
End Sub
This is also doing the same thing. :(
Pradeep
Re: Selecting Text in a Standard Textbox - Cursor blinking problem
Make your textbox multiline and it will do what you want
Re: Selecting Text in a Standard Textbox - Cursor blinking problem
Yes it works perfectly :thumb:
Is it somehow possible for a non-multiline textbox? I don't want the user to have multiline text in the box.
Pradeep :)
Re: Selecting Text in a Standard Textbox - Cursor blinking problem
Just add this code to prevent user from entering multilines
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc(vbCr) Then KeyAscii = 0
End Sub
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:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc(vbCr) Then KeyAscii = 0
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
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:
Option Explicit
Private Declare Function ShowScrollBar Lib "user32" (ByVal hwnd As Long, ByVal wBar As Long, ByVal bShow As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any _
) As Long
Private Const SB_HORZ = 0
Private Const EM_SETSEL = &HB1
Private Sub Command1_Click()
Dim StartPos As Long
Dim EndPos As Long
Text1.Text = "Helloooooooooooooo Worlddddddddddddddddddddddddd"
StartPos = InStr(Text1.Text, "World") - 1
EndPos = Len(Text1.Text)
SendMessage Text1.hwnd, EM_SETSEL, EndPos, ByVal StartPos
Text1.SetFocus
End Sub
Private Sub Form_Load()
ShowScrollBar Text1.hwnd, SB_HORZ, 0
End Sub
Private Sub Text1_Change()
If Me.TextWidth(Text1.Text) > Text1.Width Then
ShowScrollBar Text1.hwnd, SB_HORZ, 1&
Else
ShowScrollBar Text1.hwnd, SB_HORZ, 0&
End If
End Sub
hope this helps you.
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.
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.
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
Re: Selecting Text in a Standard Textbox - Cursor blinking problem
What is wrong with this?
VB Code:
Option Explicit
'Set TExt1.Multiline = true
'Set TExt.ScrollBArs = 1- Horizontal
Private Declare Function ShowScrollBar Lib "user32" (ByVal hwnd As Long, ByVal wBar As Long, ByVal bShow As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any _
) As Long
Private Const SB_HORZ = 0
Private Const EM_SETSEL = &HB1
Private Sub Command1_Click()
Dim StartPos As Long
Dim EndPos As Long
StartPos = InStr(Text1.Text, "World") - 1
EndPos = Len(Text1.Text)
SendMessage Text1.hwnd, EM_SETSEL, EndPos, ByVal StartPos
Text1.SetFocus
End Sub
Private Sub Form_Load()
ShowScrollBar Text1.hwnd, SB_HORZ, 0
Text1.Text = "Helloooooooooooooo Worlddddddddddddddddddddddddd"
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc(vbCr) Then KeyAscii = 0
End Sub
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
Re: Selecting Text in a Standard Textbox - Cursor blinking problem
Yes! It's working :thumb:
Thanks,
Pradeep :)