Results 1 to 8 of 8

Thread: How to display tool control at the caret

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    How to display tool control at the caret

    Hello.

    My code editing program, with text colouring and lots more, is almost finished.
    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?

  2. #2
    Lively Member
    Join Date
    Jan 2010
    Location
    Republic of Macedonia
    Posts
    114

    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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    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?

  4. #4
    Lively Member
    Join Date
    Jan 2010
    Location
    Republic of Macedonia
    Posts
    114

    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)

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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:
    1. Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
    2.         ' Get index of current caret position
    3.         Dim charIndex As Integer = RichTextBox1.SelectionStart
    4.  
    5.         ' Retrieve its location (relative to RTB location!)
    6.         Dim listBoxLocation As Point = RichTextBox1.GetPositionFromCharIndex(charIndex)
    7.  
    8.         'offset to RTB location
    9.         listBoxLocation.Offset(RichTextBox1.Location)
    10.  
    11.         'offset to text height (otherwise it obscures text)
    12.         listBoxLocation.Offset(0, RichTextBox1.Font.Height + 2)
    13.  
    14.         ListBox1.Location = listBoxLocation
    15.     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.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    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

  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width