Jump to content

Add element in a ComboBox if it's not in the ComboBox data


Recommended Posts

Good morning :)

I'm working on a little project, and I was wondering if there's a way ( sure there is ) to insert data to a ComboBox control everytime the user insert some text in the ComboBox which is not in the ComboBox data. I'll try to explain with an example:

- Form with some data ( ComboBox is empty );

- User prompt something in the ComboBox: if what the user prompts is not in the ComboBox values ( at the moment empty ), then insert the value in the ComboBox values, in order to have, next time the user prompts the form, the value that he entered.

More pratical example:

- First call of the form, ComboBox empty;

- I prompt "A" in the ComboBox;

- Second call, in the ComboBox I should see "A";

- I prompt "B" in the ComboBox;

- Third call, I should see "A"
                                           "B";

- And so on...

Everytime the form is called, I re-create it, so I think I can't use _GUICtrlComboBox_AddStrings().

I tried with

Global $strCboStrings = ""

; When the user prompt the form, the code below is executed.
; I.E. : User prompt "A", after the if I should have "A";
; In the second call, If I write "B", I should see "A"
;                                                  "B";
; If I write "A" again, in the combobox values should not be any changes.

If Not StringInStr($strCboStrings, GUICtrlRead($cboVoiceCategory)) <> 0 Then
    $strCboStrings &= $strCboStrings & "|" & GUICtrlRead($cboVoiceCategory)
EndIf

Any suggestion?

Thanks :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

  • Moderators

FrancescoDiMuro,

I would do something like this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

; Create a Global variable to hold the combo data
Global $sComboData = ""
; Create placeholders for the child GUI and controls
Global $hChild = 9999, $cCombo = 9999, $cAdd = 9999

; Create main GUI
$hGUI = GUICreate("Test", 500, 500)

; You say you recreate the combo each time, so this will do just that
$cAction = GUICtrlCreateButton("Action", 10, 10, 80, 30)

GUISetState()

While 1

    $aMsg = GUIGetMsg(1)
    Switch $aMsg[1]
        Case $hGUI
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    Exit
                Case $cAction
                    ; Create a child GUI
                    _CreateChild()
            EndSwitch
        Case $hChild
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    ; Close the child GUI
                    GUIDelete($hChild)
                Case $cAdd
                    ; Add new data to the combo
                    _AddComboData()
                Case $cCombo
                    ; Just to show that the data is selectable
                    MsgBox($MB_SYSTEMMODAL, "Selected", GUICtrlRead($cCombo))
            EndSwitch
    EndSwitch

WEnd

Func _CreateChild()

    ; Create child GUI
    $hChild = GUICreate("Child", 250, 250, -1, -1, $GUI_SS_DEFAULT_GUI)
    ; Create combo and load with saved data
    $cCombo = GUICtrlCreateCombo("", 10, 10, 200, 20)
    GUICtrlSetData($cCombo, $sComboData)
    ; Create a button to add new data
    $cAdd = GUICtrlCreateButton("Add", 10, 50, 80, 30)

    GUISetState()

EndFunc

Func _AddComboData()

    ; Read the content of the combo edit
    $sComboSel = GUICtrlRead($cCombo)
    ; Check if in saved data
    If Not StringInStr($sComboData, "|" & $sComboSel) Then
        ; If not then add to saved data and load into the combo
        $sComboData &= "|" & $sComboSel
        GUICtrlSetData($cCombo, $sComboData)
    EndIf

EndFunc

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

×
×
  • Create New...