Cursor Position after AutoCompletion in Rich Text Box

Posted on 2006-04-02
Last Modified: 2008-02-01
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

Question by:teamels
    LVL 14

    Accepted 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

    Author Comment

    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

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    either24  challenge 19 73
    java continue statement 10 53
    mapShare challenge 13 41
    python sqlite question 11 25
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now