Jump to content

ComboBox data insert


WhyTea
 Share

Recommended Posts

(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

  • Moderators

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:

#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

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

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:

#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

  • Moderators

WhyTea,

But I would also like the insertion to go into the top of the list

If you want that - why not try and do it yourself? :evil:

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

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

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 to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

  • Moderators

funkey,

But where does that code limit the number of elements to a pre-set maximum as the the OP wanted?

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

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 to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

  • Moderators

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". :evil:

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

  • Recently Browsing   0 members

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