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

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.

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

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
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
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

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.