Results 1 to 5 of 5

Thread: AutoComplete on multi-word RTB

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    AutoComplete on multi-word RTB

    Hey,

    I'm trying to find a way to make a sort-of AutoComplete function in my RichTextBox.
    I did some searched for AutoComplete but all I found was examples for a one-line textbox where the user only types one word. It only worked for the first word.

    In my RTB I can easily have up to 3000 lines of text so those examples don't work for me.

    What I'm after exactly is very similar to the VB6 IDE, I don't know what it's called (not intellisense) but I mean the listbox-kind-of-thing that pops up if you type a ".".
    For example:
    Text1. will give a listbox with all methods, properties etc of Text1.

    Only I don't want it to popup when you type "." but I want it to popup just as a normal AutoComplete function does.

    For example, the program has the words 'hello', 'hi', 'help' and 'start' in it's 'memory'.
    When the user types a "h" in the RTB, I want a listbox to popup near the letter "h" (or if that's too hard maybe just in a fixed place on the form) which lists "hello", "hi", and "help".

    Also, as in normal AutoCompletion, the "h" should be extended to "hello" where the bold part is selected.

    Can this be done? Are there any examples out there maybe?
    It would help already if I knew the name of this behaviour in the VB6 IDE...

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: AutoComplete on multi-word RTB

    i like the idea of this, but it looks like a huge task to populate a list box with all the words that could start with any particular letter

    think you could store the all the words in a database, then just return so many (maybe max of 20) with a query each time a key is pressed, so by the time the third or 4th letter is pressed the selection might be meaningful to select from, the listbox could show over the richtextbox based
    yon the cursor position

    if this is to just be all words you are going to need to download a good dictionary, then you can allow the users to add to the database, if your list of words is to be a special list you would have to create your own
    you could use certain keypresses to select from the listbox ie space, comma or stop and some others as well as clicking the item
    also while you need the listbox to display over the richtext, you still need the richtext to have focus to accept keystrokes

    i don't belive you could put all the words in a listbox unless you are only using a small selection words, but if that was the case you could use the sendmessage api with LBFindString to do the autocomplete part
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: AutoComplete on multi-word RTB

    I don't want it to autocomplete on all words, I have a listed of... maybe 150 words, that's all.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: AutoComplete on multi-word RTB

    Well I started writing some code and I actually have something that already works very nice! But there are some flaws I can't seem to fix

    The code:
    vb Code:
    1. Option Explicit
    2.  
    3. Dim sKeyWord(4) As String
    4. Dim Change As Boolean
    5.  
    6. Private Sub Form_Load()
    7.     sKeyWord(0) = "game_manager"
    8.     sKeyWord(1) = "spawn"
    9.     sKeyWord(2) = "setstate"
    10.     sKeyWord(3) = "underconstruction"
    11.     sKeyWord(4) = "invisible"
    12. End Sub
    13.  
    14. Private Sub rtb_Change()
    15. Dim lSpacePos As Long
    16. Dim sWord As String
    17. Dim i As Integer
    18. Dim sSelText As String, lSelStart As Long
    19.  
    20.     With rtb
    21.         'Spacepos:
    22.         If .SelStart = 0 Then Exit Sub
    23.         lSpacePos = InStrRev(.Text, " ", .SelStart)
    24.         If lSpacePos = 0 Then Exit Sub
    25.         'Word:
    26.         sWord = Mid$(.Text, lSpacePos + 1, .SelStart - lSpacePos)
    27.        
    28.         If Not sWord = "" Then
    29.             cmb.Clear
    30.             For i = 0 To 4
    31.                 If sKeyWord(i) Like sWord & "*" Then
    32.                     cmb.AddItem sKeyWord(i)
    33.                     cmb.Text = cmb.List(0)
    34.                     cmb.SelLength = Len(cmb.List(0))
    35.                    
    36.                     lSelStart = .SelStart
    37.                    
    38.                     sSelText = Mid$(cmb.List(0), Len(sWord) + 1, Len(cmb.List(0)) - Len(sWord))
    39.                     .SelText = sSelText
    40.                     .SelStart = lSelStart
    41.                     .SelLength = Len(sSelText)
    42.                     .SetFocus
    43.                    
    44.                 End If
    45.             Next
    46.         Else
    47.             cmb.Clear
    48.         End If
    49.     End With
    50.    
    51. End Sub

    rtb is a richtextbox and cmb is a combobox.

    When I type the "s" for example, the word gets autocompleted by "spawn" and the combobox gets filled with "spawn" and "setstate".

    The problems:
    - I cannot use backspace or delete for some reason after it autocompleted a word
    - it doesn't work if there's no space infront. So it doesn't work after a linebreak or at the first position...

    What am I doing wrong?

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: AutoComplete on multi-word RTB

    OK another update.

    I got rid of the combobox, I now just let the RTB show the first keyword that matches.

    The code:
    vb Code:
    1. Dim lSpacePos As Long
    2. Dim sWord As String
    3. Dim i As Integer
    4. Dim sSelText As String, lSelStart As Long
    5. Dim iWordCount As Integer
    6.  
    7. If blnBackSpace = True Then
    8.     blnBackSpace = False
    9.     Exit Sub
    10. End If
    11.    
    12.    
    13. With SOLO_RTBSyntax1
    14.     'Spacepos
    15.     If .SelStart = 0 Then Exit Sub
    16.    
    17.     lSpacePos = InStrRev(.Text, " ", .SelStart)
    18.     If lSpacePos = 0 Then
    19.         lSpacePos = InStrRev(.Text, vbNewLine, .SelStart)
    20.         If lSpacePos = 0 Then
    21.             lSpacePos = InStrRev(.Text, "", .SelStart)
    22.             If lSpacePos = 0 Then Exit Sub
    23.         End If
    24.     End If
    25.            
    26.     'Word:
    27.     sWord = Mid$(.Text, lSpacePos + 1, .SelStart - lSpacePos)
    28.    
    29.     If Not sWord = "" Then
    30.         For i = 0 To 3
    31.             If sKeyWord(i) Like sWord & "*" Then
    32. '                iWordCount = iWordCount + 1
    33. '                If Not iWordCount > 1 Then
    34.                     lSelStart = .SelStart
    35.                     sSelText = Mid$(sKeyWord(i), Len(sWord) + 1, Len(sKeyWord(i)) - Len(sWord))
    36.                     .SelText = sSelText
    37.                     .SelStart = lSelStart
    38.                     .SelLength = Len(sSelText)
    39.                     '.SetFocus
    40.                     Exit For
    41. '                End If
    42.             End If
    43.         Next
    44.     End If
    45. End With

    It works 99% perfectly.

    The only flaws:
    - It doesn't work when I try it at the very first position of the RTB (.SelStart = 0, and InStrRev cannot start from 0).
    - It only works after a space. Not after a tab, newline etc.

    The nested If loops with lSpacePos are not functioning correctly, I know that much. They are there to see where the (part of the word) you are typing has started (or, where the first space, newline or tab occured).
    However it should only find the space, newline or tab that is right in front of the word you're typing.
    Now, the spacepos is from a totally different space, somewhere a few lines up when there was actually a tab infront of the word for example...
    Last edited by NickThissen; Jul 23rd, 2007 at 12:03 PM.

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