Jump to content

ComboBox Partial Match as default


Go to solution Solved by Melba23,

Recommended Posts

Posted

Hello,

I wonder if there is an existing solution (may aold post I overlooked?) that handles "partial combobox match set as default" ?

Eg: A dynamic filled combobox contains items like

(3 items)

item-1-a

item-2-a

item-3-a

And now I want to set a item as default by not knowing exactly the item text.

As an example I need to set the item "item-2" as the default.

With the build in I need to know the exact string right?

So here GUICtrlSetData(-1, "item-1-a|item-2-a|item-3-a","item-2-a")

But I need something like

GUICtrlSetData(-1, "item-1-a|item-2-a|item-3-a","item-2*") (the asterix on the ned indicates "any chars".

The combo is created with the build in method GUICtrlCreateCombo if this is important not by the _udf

My current idea is to build a function that loops over all items and do a partial compare and returns than the index. But before creating this, I want to know if this has been done before. Re-inventing the wheel does not make much sense.

#include <GUIConstantsEx.au3>

Example()

Func Example()
    Local $msg

    GUICreate("My GUI") ; will create a dialog box that when displayed is centered

    GUICtrlCreateCombo("", 10, 10)

    GUICtrlSetData(-1, "item-1-a|item-2-a|item-3-a","item-2-a")

    GUISetState() ; will display an empty dialog box with a combo control with focus on

    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()

        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
EndFunc   ;==>Example
  • Moderators
  • Solution
Posted

Tankbuster,

I would do it like this:

#include <GUIConstantsEx.au3>
#include <Array.au3>

Example()

Func Example()
    Local $msg

    ; Put combo items into an array
    Local $aCombo_Data[3] = ["item-1-a", "item-2-a", "item-3-a"]
    Local $sMatch = "item-1"
    ; Look for a partial match
    Local $sDefault
    Local $iIndex = _ArraySearch($aCombo_Data, $sMatch, 0, 0, 0, 1) ; Do partial search
    ; Set a default
    If @error Then
        $sDefault = ""
    Else
        $sDefault = $aCombo_Data[$iIndex]
    EndIf
    ; Get the data into a form to insert
    Local $sCombo_Data = _ArrayToString($aCombo_Data, "|")

    GUICreate("My GUI") ; will create a dialog box that when displayed is centered

    GUICtrlCreateCombo("", 10, 10)

    GUICtrlSetData(-1, $sCombo_Data, $sDefault)

    GUISetState() ; will display an empty dialog box with a combo control with focus on

    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()

        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
EndFunc   ;==>Example
Seems to work when I test it. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)

Hey I knew it, it must be that easy (....if you are Melba  :sorcerer:  )

Thx. Much better than "looping". Nice _Arraysearch.

 

Next time I expect you to be even faster (post the solution before my question .......)

//edit: I just found the "Mark solved" button, nice never saw that before....

Edited by Tankbuster

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
  • Recently Browsing   0 members

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