Jump to content

_GUICtrlComboBox_SelectString & StringInStr to Select Item matching substring(Not at beginning of string)


Recommended Posts

Hello. I am attempting to select an item in a ComboBox that matches a substring. I am only able to match from the beginning of the ComboBox item string, but I would like to match my search substring to any part of the item string. _GUICtrlComboBox_SelectString will not work as it can only match from the beginning of an item string. Thank You in advance!

Link to comment
Share on other sites

#include <GuiComboBox.au3>

$hGUI = GUICreate('Test')
$hCombo = _GUICtrlComboBox_Create($hGUI, '', 100, 100, 150, 30)
GUISetState(@SW_SHOW, $hGUI)

For $Index = 1 To 10
    _GUICtrlComboBox_AddString($hCombo, 'Item ' & $Index)
Next

SelectString($hCombo, 'tem 5')

Do
Until GUIGetMsg() = -3

Func SelectString($hCombo, $sText)
    Local $aItem = _GUICtrlComboBox_GetListArray($hCombo)
    For $Index = 1 To $aItem[0]
        If StringInStr($aItem[$Index], $sText) Then
            Return _GUICtrlComboBox_SelectString($hCombo, $aItem[$Index])
        EndIf
    Next
    Return SetError(1, 0, -1)
EndFunc

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Thanks to @Andreik

#include <GuiComboBox.au3> ; _GUICtrlComboBox_Create

Opt("MustDeclareVars", 1)

Local $_Lh_gui = GUICreate("Find Substring in ComboBox", 400, 200)
Local $_Lc_cmb_items = _GUICtrlComboBox_Create($_Lh_gui, '', 10, 10, 150, 20)
Local $_Lc_inp_find = GUICtrlCreateInput("Item", 170, 10, 100, 20)
Local $_Lc_btn_search = GUICtrlCreateButton("Search", 280, 10, 100, 20)
Local $_Ls_search_text
Local $_Ls_found
Local $_Lc_lbl_output = GUICtrlCreateLabel("Items found:", 10, 40, 380, 150)

GUISetState(@SW_SHOW, $_Lh_gui)

For $Index = 1 To 10
    _GUICtrlComboBox_AddString($_Lc_cmb_items, 'Item ' & $Index)
Next

While 1
    Switch GUIGetMsg()
        Case -3 ; $GUI_EVENT_CLOSE
            ExitLoop

        Case $_Lc_btn_search
            $_Ls_search_text = GUICtrlRead($_Lc_inp_find)
            $_Ls_found = _Find_String($_Lc_cmb_items, $_Ls_search_text) ; Find String
            GUICtrlSetData($_Lc_lbl_output, "Items found:" & @CRLF & $_Ls_found)
    EndSwitch
WEnd

Exit

Func _Find_String($rc_cmb_items, $rs_search_text) ; Find String
    Local $la_item = _GUICtrlComboBox_GetListArray($rc_cmb_items)
    Local $ls_found_items = ""
    For $Index = 1 To $la_item[0]
        If StringInStr($la_item[$Index], $rs_search_text) Then
            $ls_found_items &= $la_item[$Index] & @CRLF
        EndIf
    Next

    If $ls_found_items = "" Then
        Return SetError(1, 0, "No items found.")
    Else
        Return $ls_found_items
    EndIf
EndFunc ; ==> _Find_String

 

"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Link to comment
Share on other sites

On 3/15/2024 at 7:16 AM, Andreik said:
#include <GuiComboBox.au3>

$hGUI = GUICreate('Test')
$hCombo = _GUICtrlComboBox_Create($hGUI, '', 100, 100, 150, 30)
GUISetState(@SW_SHOW, $hGUI)

For $Index = 1 To 10
    _GUICtrlComboBox_AddString($hCombo, 'Item ' & $Index)
Next

SelectString($hCombo, 'tem 5')

Do
Until GUIGetMsg() = -3

Func SelectString($hCombo, $sText)
    Local $aItem = _GUICtrlComboBox_GetListArray($hCombo)
    For $Index = 1 To $aItem[0]
        If StringInStr($aItem[$Index], $sText) Then
            Return _GUICtrlComboBox_SelectString($hCombo, $aItem[$Index])
        EndIf
    Next
    Return SetError(1, 0, -1)
EndFunc

 

Thank You!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...