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

Cursor Position after AutoCompletion in Rich Text Box

Using VB.NET 2003

I have an auto completion list box programmed to pop up when certain words are typed in the Rich Text Box.  I am having difficulty repositioning the cursor after the new word has been entered into the RTB when using the Tab or Enter keys.

I start to type a word in the RTB - the list box comes up - I scroll down the list and highlight the word I want - I then press either the Tab or the Enter keys to replace the letters just typed into the RTB with the word selected.

When I use the Tab key the word is entered correctly, but the cursor is then placed a tabbed space away from the end of the word just entered.
Example:{The pipe symbol below represents the cursor}

NewWord     |

Or if I use the Enter key the word is added then the cursor is on the next line because the Enter key added a new line. (carriage return)


Can someone lend a hand here and let me know how I can put the cursor at the end of the word just entered regardless if the tab or enter key was used.

Thank You

Here is the code I use {Only the Tab Shown, Case Enter Key is identical}
 Case Keys.Tab
                If ListBox1.SelectedIndex > -1 Then
                    RichTextBox1.SelectionStart = start
                    RichTextBox1.SelectionLength = ((endpoint - start) + 1)
                    RichTextBox1.SelectionColor = Color.Blue
                    RichTextBox1.SelectedText = ListBox1.Items(nextCt)
                    ' How to set cursor position to just after replaced text.
                    ' Below are a few things I tried, but they are commented out now.
                    ' RichTextBox1.SelectionStart = 0
                    ' RichTextBox1.SelectionLength = endpoint
                    ' RichTextBox1.SelectionStart = start + RichTextBox1.SelectedText.Length
                End If

1 Solution
If the above code is placed inside the KeyPress event handler of the RichTextBox, then you can add the following line to ignore the pressed key:

        e.Handled = True

where e is of type System.Windows.Forms.KeyPressEventArgs, and contains the event arguments passed to the event handler.

In the special case of pressing the Enter key, it is accepted that the cursor moves to a new line. Afterall, this is what happens when you use Enter with the auto-complete feature in Visual Studio itself. :-)


  Nayer Naguib
teamelsAuthor Commented:
Yes this works, thank you

 Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
        If Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

I will still need to refine this a bit so I can use the Tab key as normal when there is no word selected in the listbox, which is not a problem.

Thanks for your help
Nayer Naguib

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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