Sign in to follow this  
Followers 0
Herb191

How to make an autocomplete drop down box in a Rich Edit control

4 posts in this topic

#1 ·  Posted (edited)

I would like to create autocomplete drop down box that works in a Rich Edit control something similar to the one in SciTE. So each new word would give a list of suggestions and remove any words from the list that were not relevant.

_GUICtrlComboBox_AutoComplete() won’t work because it creates a input box.

I can't come up with any solutions that I like to make this work. Any suggestions would be greatly appreciated.

Thanks

Edited by Herb191

Share this post


Link to post
Share on other sites



Well this is what I came up with. Any constructive criticism welcome.

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GuiRichEdit.au3>
#include <GuiListBox.au3>

Global $iStart = 0, $iEnd = 0, $iIndex = 0

Dim $aWordList[13]
$aWordList[0] = "AAAAAA"
$aWordList[1] = "AAAAAB"
$aWordList[2] = "AAAABB"
$aWordList[3] = "AAABBB"
$aWordList[4] = "AABBBB"
$aWordList[5] = "BBBBBB"
$aWordList[6] = "CCCCCCC"
$aWordList[7] = "CCDDDD"
$aWordList[8] = "DDDDDD"
$aWordList[9] = "DDAAAA"
$aWordList[10] = "ABCDE"
$aWordList[11] = "EDBA"
$aWordList[12] = "VVVVVVV"

$MainGui = GUICreate("Test", 400, 296)
$hRichEdit = _GUICtrlRichEdit_Create($MainGui, 'Some text', 5, 5, 390, 220, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
_GUICtrlRichEdit_SetModified($hRichEdit, False)

$hList = GUICtrlCreateList("test", 0, 0, 100, 100)

For $i = 0 To UBound($aWordList, 1) - 1
    GUICtrlSetData($hList, $aWordList[$i] & "|")
Next

ControlHide($MainGui, "", $hList)
$bListHidden = True

GUISetState(@SW_SHOW)

Do
    If _GUICtrlRichEdit_IsModified($hRichEdit) Then
        ShowAndUpdateList()
    EndIf
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func ShowAndUpdateList()
    $array = _GUICtrlRichEdit_GetSel($hRichEdit)

    ;finds last edited text
    $iStart = _GUICtrlRichEdit_GetCharPosOfPreviousWord($hRichEdit, $array[0])
    $iEnd = _GUICtrlRichEdit_GetCharPosOfNextWord($hRichEdit, $array[0])
    $sText = _GUICtrlRichEdit_GetTextInRange($hRichEdit, $iStart, $iEnd)

    ;find the index of the string to seach for
    $iIndex = _GUICtrlListBox_FindString($hList, $sText)

    ;if string not found hide list
    If $iIndex < 0 Then
        ControlHide($MainGui, "", $hList)
        $bListHidden = True
        HotkeyOn(False)
        Return ""
    EndIf

    Select
        Case $sText <> "" And $bListHidden = True
            $aListPos = _GUICtrlRichEdit_GetXYFromCharPos($hRichEdit, $iStart)

            ControlMove($MainGui, "", $hList, $aListPos[0], $aListPos[1] + 20)

            _GUICtrlRichEdit_SetModified($hRichEdit, False)

            _GUICtrlListBox_SetCurSel($hList, 0)
            ControlShow($MainGui, "", $hList)
            $bListHidden = False
            HotkeyOn(True)

            _GUICtrlListBox_SetCurSel($hList, $iIndex)

            _WinAPI_RedrawWindow($MainGui)
        Case $bListHidden = False

            ControlHide($MainGui, "", $hList)
            $bListHidden = True
            HotkeyOn(False)

            _WinAPI_RedrawWindow($MainGui)
        Case Else
            _GUICtrlListBox_SetCurSel($hList, $iIndex)
    EndSelect
EndFunc   ;==>ShowAndUpdateList

Func HotkeyOn($bOn)
    If $bOn Then
        HotKeySet("{UP}", "ListUp")
        HotKeySet("{DOWN}", "ListDown")
        HotKeySet("{ENTER}", "ListEnter")
    Else
        HotKeySet("{UP}")
        HotKeySet("{DOWN}")
        HotKeySet("{ENTER}")
    EndIf
EndFunc   ;==>HotkeyOn

Func ListUp()
    If $iIndex > 0 Then $iIndex = $iIndex - 1
    _GUICtrlListBox_SetCurSel($hList, $iIndex)
EndFunc   ;==>ListUp

Func ListDown()
    $iIndex = $iIndex + 1
    _GUICtrlListBox_SetCurSel($hList, $iIndex)
EndFunc   ;==>ListDown

Func ListEnter()
    _GUICtrlRichEdit_SetSel($hRichEdit, $iStart, $iEnd)

    $sSelText = _GUICtrlListBox_GetText($hList, _GUICtrlListBox_GetCurSel($hList))

    _GUICtrlRichEdit_ReplaceText($hRichEdit, $sSelText & " ")
EndFunc   ;==>ListEnter
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0