How to display tool control at the caret
Hello.
My code editing program, with text colouring and lots more, is almost finished. :D
It uses a rich text box to edit in.
On the right is a listbox, called the "suggestion box"
The suggestion listbox is kinda large and eats up lots of valuable edit space :(
How can I display my "suggestion listbox" control underneath the caret, like in Visual Basic?
Re: How to display tool control at the caret
Not very efficient but will work:
Code:
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Me.ListBox1.Location = e.Location
End Sub
Re: How to display tool control at the caret
That won't work, because then it places it at the mouse cursor...
It should display underneath the ][ caret cursor.
Any help?
Re: How to display tool control at the caret
I am not sure i understand your explanation but please try this:
Code:
Me.ListBox1.Location = New Point(e.X, e.Y + 20)
Re: How to display tool control at the caret
He means underneath the point where the user is typing, not where his mouse is, just like the Intellisense popup in Visual Studio.
You can find that location using a combination of the SelectionStart property and some of the methods of the RTB. I can't really remember their name but they all start with Get.. Something like GetPositionAtCharIndex or something like that.
EDIT
Just had a look for you (turns out the name was close). Try something like this
vb.net Code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
' Get index of current caret position
Dim charIndex As Integer = RichTextBox1.SelectionStart
' Retrieve its location (relative to RTB location!)
Dim listBoxLocation As Point = RichTextBox1.GetPositionFromCharIndex(charIndex)
'offset to RTB location
listBoxLocation.Offset(RichTextBox1.Location)
'offset to text height (otherwise it obscures text)
listBoxLocation.Offset(0, RichTextBox1.Font.Height + 2)
ListBox1.Location = listBoxLocation
End Sub
It will be a little more complicated because I suppose you want to hide and show it depending on whether or not there is any items in it at all.
Re: How to display tool control at the caret
WOW
Exactly what I wanted! It even adapts to the font and character size!
Thank you so, so much!
You're Dutch too? =P
Re: How to display tool control at the caret
You can even go a bit further and show the listbox at the start of the current word, instead of at the caret. For that you'd need to loop back through the characters in the current line until you find a space, newline, tab, etc. Then, the listbox will stay in one place while you type a word and it should jump to the new word when you type a space (I think that's how it behaves in visual studio).
And yes, I'm dutch too.
Re: How to display tool control at the caret
Hehe I did exactly the same as you said; loop up and jump out of the loop if the character (space or '.') is found and jump out if no character can be found.
Great to know there are more dutch programmers =P