Select text backwards - so that the current cursor is at the beginning of the selected text.

-- VB6 --
I have the need to select text in a ComboBox so that after selecting the text, the current cursor location is at the front of the selection as opposed to the end of the selection.

The ComboBox has the SelStart and SelLength properties.  The SelLength must be 0 or greater (therefore I cannot select a negative length to make the selection go backwards).

I tried sending the CB_SETEDITSEL message to the ComboBox but it would always select the 19th character to the end of line no matter what I passed as a W and L Param.  I did put the start and end in the LParam.

I tried getting the handle of the Edit Control associated with the ComboBox and sending the EM_SETSEL message to it.  I had a little more success in that I could get it to select the range but it would always put the cursor at the end of the selection.  I tried sending a start char of 3 and end char of 7 - and it acted correct.  I tried sending a start char of 7 and end char of 3 - and it acted the same as 3 and 7 in that it hilited the correct text but still put the cursor at the end of the selection.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this little test and see if it solves your problem

Private Sub Command1_Click()
   Dim mFind As String
   Dim mResult As String
   mFind = InputBox("Find What?")
   mResult = InStr(Combo1.Text, mFind)
   If mResult Then
      Combo1.SelStart = mResult - 1
      Combo1.SelLength = 1
   End If
End Sub

Private Sub Form_Load()
   Combo1.Text = "Test for finding selection in combo box"
End Sub
markh524Author Commented:
Your suggestion tells me that I should rephrase my question another way.

I want a multi-character selection but I want the caret (flashing cursor) to be at the front of the selection instead of at the end of the selection.

When at tried jgarth's suggestion, it just selected the first character.
This is an interesting request and the subroutine below should help out. The only problem with it is it has to set focus to the text box to do it because of the reliance on SendKeys. It's possible to emulate keyboard events directly to the control without having it in focus, using an API call.

Can I ask why it's necessary to have the cursor at the beginning of the selection? I cannot think of a scenario in which it would matter...

Private Sub ReverseSelect(txtBox As TextBox, SelStart As Long, SelLength As Long)

    Dim Iter As Long
    txtBox.SelStart = SelStart
    For Iter = 1 To SelLength
        SendKeys "+{LEFT}"

End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ignore my last. Here's some API code that does it easier:


    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Private Const EM_SETSEL = &HB1

Event Code:

Private Sub Command1_Click()
    Dim SelStart As Long
    Dim SelEnd As Long

    SelStart = 20
    SelEnd = 5

    PostMessage Text1.hwnd, EM_SETSEL, SelStart, SelEnd
End Sub

Notice that the SelStart and SelEnd are actual character positions. The code above selects 15 characters starting from character position 20 going backward to character position 5.
markh524Author Commented:
JCinDE - The SendKeys solution worked for what I requested.

I had tried the EM_SETSEL solution before (and retried your solution) but it still put the caret at the end of the selection.

What I discovered was that I posted the wrong question -so- I will repost a more accurate (separate thread) question for additional points (has to do with wanting to horizontally scroll what is visible in the Combo text area so that the first part is always visible - I thought moving the cursor to the front would do that for me).
If the SelStart is greater than the SelEnd when calling the API I described then the blinking cursor will be at the left end of the selection. If the SelStart is smaller than the SelEnd then the cursor will be at the right end of the selection.

markh524Author Commented:
That's what the API says but I still can't get it to work.
I started over with a new project and it still puts the flashing caret at the end (right side) of the selection.
I even added:
   Text1.SetFocus that I could see the selection without having to tab to the control to see it.

I compiled and ran the .exe (with and without the SetFocus) incase it was something goofy while running interactively.

FYI - This is VB6 running on Win2K.
markh524Author Commented:
Accompanying question referred to in my 2:56PM post is entitled, "Horizontally Scroll Text in ComboBox - leaving the currently selected text alone." - Q_20783500.html
Doh! Sorry. I just found this on MSDN's site:

    Edit controls: The control displays a flashing caret at the end position regardless of the relative values of start and end.

So that means the EM_SETSEL doesn't work afterall.

Anyway, for your actual question, try this API:

    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Const EM_SCROLLCARET = &HB7

Private Sub cmdTest_Click()

    Dim SelStart As Long
    Dim SelLength As Long
    SelStart = Val(txtStart.Text)
    SelLength = Val(txtLength.Text)
    ReverseSelect txtSingle, SelStart, SelLength
    PostMessage txtSingle.hwnd, EM_SCROLLCARET, 0, 0
    ReverseSelect txtMulti, SelStart, SelLength
    PostMessage txtMulti.hwnd, EM_SCROLLCARET, 0, 0
End Sub

This should scroll the textbox so the caret comes into view.
markh524Author Commented:
Ahhh, but I discovered that if you change an Edit control's style to ES_MULTILINE that it the EM_SETSEL will work.
I tried to change a ComboBox's associated Editbox's style to ES_MULTILINE but then no controls on the form responded.
I'll stick with the accepted answer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.