• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

How to set the caret/cursor at the beginning of the selected text instead of at the end?

I have a textbox in which commands are typed. Using the left and right arrows I want to allow the user to select the text  from the cursor up to the end of the text.
This functionality equals the shift + left/right arrow standard functionality, but I want to be able to do it without the shift pressed, because I want to make sure the user cannot just place the cursor anywhere in the text.
The problem is that when you set a selection, either using selectionstart and selectionlength or select(start,length), the caret/cursor is always positioned at the end of the selection and no matter what I do, I cannot get it at the beginning of the selection, where I want it to be.

There must be a way since the standard functionality using shift left/right arrow works just fine, but I don't know how.
0
Paybit
Asked:
Paybit
  • 3
  • 2
  • 2
  • +2
1 Solution
 
RonaldBiemansCommented:
I think that is by design, I don't think you can do anything about that.
0
 
Bob LearnedCommented:
Yes, I second that, so good luck in overriding that behavior :)

Bob
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can make a "backwards" selection via SendKeys() so that it is achieved the same you would doing it manually with the keyboard using Shift+Left:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = "0123456789"
        SelectBackwards(TextBox1, 9, 4) ' selects "678" with cursor before the "6"
    End Sub

    Private Sub SelectBackwards(ByVal tb As TextBox, ByVal startPosition As Integer, ByVal length As Integer)
        ' I haven't added any intelligent error checking here...
        Dim i As Integer
        tb.SelectionStart = startPosition
        tb.SelectionLength = 0
        For i = 1 To length
            SendKeys.Send("+{LEFT}")
        Next
    End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PaybitAuthor Commented:
Nice try Idle Mind, but it doesn't work from a keyup event.
You get an unending loop since the sendkey triggers the keyup of the left arrow which again sends the key, which again triggers the event ... and so on... I'm sure it works alright in a click event, but not in the event handling the key sent, which is where I need it.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Alrighty...don't think I quite understood exactly what you were after then.

Could you give an example of what might be in the textbox and how you would want the user to interact with that text and/or how you need to set the selection/cursor position programatically?
0
 
PaybitAuthor Commented:
The textbox is a commandline. when user type in the box, a list with possible commands is shown and the command closest to what the user has typed is selected. On pressing space or enter, the selected command is copied into the box (autofill). a command is built up from different parts. First you have an action you want to perform (like for example show, alter, delete or add)  next you give the object on which you want to perform the action (say for example an article or address) on the object and optionally you can add a parameter (a number or string to identify one or more objects).
You get commands like "delete article 10" or "show client a%". In order to assure the validity of the commands and making sure that when you move trough the box with the arrow keys, the selectionlist shows the correct list and pressing a key will trigger the autoselect int he list and/or the autofill, I must make sure everything from the end of the box to the caret is selected so that it will be deleted/replaced.
0
 
Fenris_LokissonCommented:
This is more a workaround than a real solution as it will select the text correctly yet the caret remains at the end.
First make 2 variables to hold the values of the selectionlength and the current start of the selection.

Private intSelLength As Integer = 0
Private intSelStart As Integer = -1

Create a function to shift the selection

  Private Sub ShiftTextSelection(ByVal txtTextBox As TextBox)
    'Set initial vars
    If (Me.intSelStart < 0) Then
      Me.intSelStart = txtTextBox.Text.Length
      Me.intSelLength = 0
    End If

    With txtTextBox
      'SelectionStart
      Dim intNewSelStart As Integer = Me.intSelStart - 1
      If (intNewSelStart < 0) Then intNewSelStart = 0
      .SelectionStart = intNewSelStart
      Me.intSelStart = .SelectionStart

      'SelectionLength
      Dim intNewSelLength As Integer = Me.intSelLength + 1
      If (intNewSelLength > txtTextBox.Text.Length) Then intNewSelLength = txtTextBox.Text.Length
      .SelectionLength = intNewSelLength
      Me.intSelLength = .SelectionLength
    End With

  End Sub

In your eventhandler call the function

Private Sub KeyUpHandler(ByVal sender As Object, ByVal e As
KeyEventArgs) Handles txtTestBox.KeyDown
    Select Case e.KeyCode
      Case Keys.Left
        If (txtTextBox.SelectionLength <> txtTextBox.Text.Length -txtTextBox.SelectionStart) Then
           Me.intSelLength = txtTextBox.Text.Length - txtTextBox.SelectionStart
           Me.intSelStart = txtTextBox.SelectionStart
        else
          Me.intSelLength = txtTextBox.SelectionLength
          Me.intSelStart = txtTextBox.SelectionStart
        End If
        e.Handled = True
        ShiftTextSelection(txtTextBox)
     End Select
  End Sub

0
 
Fenris_LokissonCommented:
BTW you could use the code of Idle Mind and avoid the loop by checking for a shift pressed in the keydown event

Private Sub KeyUpHandler(ByVal sender As Object, ByVal e As
KeyEventArgs) Handles txtTestBox.KeyDown
Select Case e.KeyCode
      Case Keys.Left
        'check if shift is pressed
        If e.Shift = False Then
          'set the left press as handled
          e.Handled = True
          'trigger shift + left
          SendKeys.Send("+{LEFT}")
        End If
     End Select
end sub

0
 
PaybitAuthor Commented:
Thanks, It's working now.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now