WhyTea Posted December 30, 2009 Share Posted December 30, 2009 (1) Is there a way to insert a string into the top of a ComboBox? (2) Is there a way to ensure the string is unique before insertion in (1)? (3) How to limit the maximum number of items in a ComboBox? If there are already max items, the last item is deleted before the new one is inserted. Thanks. /Why Tea Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 30, 2009 Moderators Share Posted December 30, 2009 WhyTea,1. Yes, use _GUICtrlComboBox_SetEditText. This can insert any text you want, it does not have to be an element of the combo and is not added to the list.2. If the combo elements are held in an array, you can use _ArraySearch to check if the proposed new element already exists.3. If you use the array method above, then _ArrayPush will do what you want.There are many other solutions to 2 & 3, but using an array lets you do both quite easily - as you can see here:expandcollapse popup#include <GUIConstantsEx.au3> #Include <GuiComboBox.au3> #include <Array.au3> ; Set max number of combo elements Global $iMaxList = 5 ; Initialise combo list Global $iComboCount = 3 Global $aComboArray[$iMaxList] = ["tom", "dick", "harry"] ; Create GUI $hGUI = GUICreate("Test", 500, 500) Global $hCombo = GUICtrlCreateCombo("", 10, 10, 250, 20) $hAddButton = GUICtrlCreateButton("Add", 270, 10, 30, 20) GUISetState() ; Fill combo with existing elements _Fill_Combo() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $hAddButton ; Read contents of combo edit box $sText = GUICtrlRead($hCombo) ; Check if this is unique If _ArraySearch($aComboArray, $sText) = -1 Then ; If it is, then check if max elements already exist If $iComboCount < $iMaxList Then ; There is room to add, so increase count $iComboCount += 1 ; Add to array $aComboArray[$iComboCount - 1] = $sText Else ; No room so we must delete an exisiting element _ArrayPush($aComboArray, $sText) EndIf ; Refill combo _Fill_Combo() EndIf EndSwitch WEnd Func _Fill_Combo() ; Clear existing combo data GUICtrlSetData($hCombo, "|") ; Loop through list inserting elements into combo For $i = 0 To $iComboCount - 1 GUICtrlSetData($hCombo, $aComboArray[$i]) Next ; Set the top element into the combo edit box _GUICtrlComboBox_SetEditText($hCombo, $aComboArray[0]) EndFuncPlease ask if anything is unclear. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
WhyTea Posted December 31, 2009 Author Share Posted December 31, 2009 WhyTea, 1. Yes, use _GUICtrlComboBox_SetEditText. This can insert any text you want, it does not have to be an element of the combo and is not added to the list. 2. If the combo elements are held in an array, you can use _ArraySearch to check if the proposed new element already exists. 3. If you use the array method above, then _ArrayPush will do what you want. There are many other solutions to 2 & 3, but using an array lets you do both quite easily - as you can see here: expandcollapse popup#include <GUIConstantsEx.au3> #Include <GuiComboBox.au3> #include <Array.au3> ; Set max number of combo elements Global $iMaxList = 5 ; Initialise combo list Global $iComboCount = 3 Global $aComboArray[$iMaxList] = ["tom", "dick", "harry"] ; Create GUI $hGUI = GUICreate("Test", 500, 500) Global $hCombo = GUICtrlCreateCombo("", 10, 10, 250, 20) $hAddButton = GUICtrlCreateButton("Add", 270, 10, 30, 20) GUISetState() ; Fill combo with existing elements _Fill_Combo() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $hAddButton ; Read contents of combo edit box $sText = GUICtrlRead($hCombo) ; Check if this is unique If _ArraySearch($aComboArray, $sText) = -1 Then ; If it is, then check if max elements already exist If $iComboCount < $iMaxList Then ; There is room to add, so increase count $iComboCount += 1 ; Add to array $aComboArray[$iComboCount - 1] = $sText Else ; No room so we must delete an exisiting element _ArrayPush($aComboArray, $sText) EndIf ; Refill combo _Fill_Combo() EndIf EndSwitch WEnd Func _Fill_Combo() ; Clear existing combo data GUICtrlSetData($hCombo, "|") ; Loop through list inserting elements into combo For $i = 0 To $iComboCount - 1 GUICtrlSetData($hCombo, $aComboArray[$i]) Next ; Set the top element into the combo edit box _GUICtrlComboBox_SetEditText($hCombo, $aComboArray[0]) EndFunc Please ask if anything is unclear. M23 Thank you. It works well. But I would also like the insertion to go into the top of the list. In the example you have given: tom dick harry An "Add" of Dicko will cause the list to become: tom dick harry dicko I'd like it to be: dicko tom dick harry Happy New Year! /Why Tea Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 31, 2009 Moderators Share Posted December 31, 2009 WhyTea, But I would also like the insertion to go into the top of the listIf you want that - why not try and do it yourself? You will have to use a loop to move the elements of the array so your new value can be added in the [0] element, but it is not very difficult - honest. Come back with your code when you run into problems. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
funkey Posted December 31, 2009 Share Posted December 31, 2009 No loop is needed. #include <GuiComboBox.au3> GUICreate("!") GUICtrlCreateCombo("", 10, 10, 200, 50) GUICtrlSetData(-1, "tom|dick|harry") $hCombo = GUICtrlGetHandle(-1) _GUICtrlComboBox_InsertString($hCombo, "dicko", 0) GUISetState() Do Until GUIGetMsg() = -3 Programming today is a race between software engineers striving tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 31, 2009 Moderators Share Posted December 31, 2009 funkey, But where does that code limit the number of elements to a pre-set maximum as the the OP wanted? M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
funkey Posted December 31, 2009 Share Posted December 31, 2009 Sorry, I havn't read all the text of the OP. #include <GuiComboBox.au3> GUICreate("!") GUICtrlCreateCombo("", 10, 10, 200, 50) GUICtrlSetData(-1, "tom|dick|harry") $hCombo = GUICtrlGetHandle(-1) _GUICtrlComboBox_InsertString($hCombo, "dicko", 0) _GUICtrlComboBox_InsertString($hCombo, "funkey", 0) _Limit_ComboBoxEntries($hCombo, 3) GUISetState() Do Until GUIGetMsg() = -3 Func _Limit_ComboBoxEntries($hCombo, $iLimit) Local $Count = _GUICtrlComboBox_GetCount($hCombo) If $Count > $iLimit Then Do Until _GUICtrlComboBox_DeleteString($hCombo, $iLimit) = -1 EndIf EndFunc Programming today is a race between software engineers striving tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 31, 2009 Moderators Share Posted December 31, 2009 funkey,Nice, but I was trying to get him/her to do something him/herself rather than just pop in a few UDFs! You know - the old "Give a man a fish vs. give a man a net" thing". M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now