[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 602
  • Last Modified:

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.
0
markh524
Asked:
markh524
  • 5
  • 4
1 Solution
 
jgarthCommented:
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
0
 
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.
0
 
JCinDECommented:
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.SetFocus
    txtBox.SelStart = SelStart
   
    For Iter = 1 To SelLength
        SendKeys "+{LEFT}"
    Next

End Sub
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Declarations:

    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
    DoEvents
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.
0
 
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).
0
 
JCinDECommented:
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.

0
 
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
...so 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.
0
 
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
0
 
JCinDECommented:
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
    DoEvents
   
    ReverseSelect txtMulti, SelStart, SelLength
    PostMessage txtMulti.hwnd, EM_SCROLLCARET, 0, 0
    DoEvents
   
    txtSingle.SetFocus
   
End Sub

This should scroll the textbox so the caret comes into view.
0
 
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.
Thanks!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now