We help IT Professionals succeed at work.

Cursor Position after AutoCompletion in Rich Text Box

teamels
teamels asked
on
Medium Priority
491 Views
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)
Example:

NewWord
|


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
teamels


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


Comment
Watch Question

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.