Editable ComboboxGadget - inserting/deleting items
Posted: Tue Sep 27, 2016 8:28 pm
As I think the following example shouldn't get "lost" inside the thread with coding questions, and I think it's of valuable help, I post it here for easier finding:
Code: Select all
; original thread: http://www.purebasic.fr/english/viewtopic.php?f=13&t=45043
; by RASHAD, extended by Andre
; Demonstration of an editable Combobox:
; - items can be deleted by pressing 'Delete' on the current selection
; - items can be added by pressing 'Insert' or 'Return' for finishing the current input
Enumeration
#InsertItem
#DeleteItem
EndEnumeration
Enumeration
#ComboBox
EndEnumeration
OpenWindow(0, 0, 0, 350, 100 , "Editable Combobox - delete+insert items", #PB_Window_SystemMenu |#PB_Window_ScreenCentered)
ComboBoxGadget(#ComboBox, 10, 10, 280, 21, #PB_ComboBox_Editable )
For a = 1 To 20
AddGadgetItem(#ComboBox, -1, "item " + Str(a))
Next
SetGadgetState(#ComboBox, 0)
AddKeyboardShortcut(#ComboBox, #PB_Shortcut_Delete, #DeleteItem)
AddKeyboardShortcut(#ComboBox, #PB_Shortcut_Insert, #InsertItem)
AddKeyboardShortcut(#ComboBox, #PB_Shortcut_Return, #InsertItem)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_Menu
Select EventMenu()
Case #DeleteItem
If GetGadgetState(#ComboBox) >= 0 ; a Combobox item was selected before pressing the 'Delete' key
Debug "Combobox item removed: " + GetGadgetText(#ComboBox)
RemoveGadgetItem(#ComboBox, GetGadgetState(#ComboBox))
EndIf
Case #InsertItem
; Get current state and item of the editable combobox:
a$ = GetGadgetText(#ComboBox)
state = GetGadgetState(#ComboBox)
If state = -1 ; Combobox is in edit-mode
; Check if such an entry is already existing in the Combobox items:
existing = #False
b = CountGadgetItems(#ComboBox)
For a = 0 To b
If GetGadgetItemText(#ComboBox, a) = a$
existing = #True
Break
EndIf
Next
; Now add the new item, if it isn't present yet:
If existing = #False
AddGadgetItem(#ComboBox, state, a$) ; as state = -1, the new item will the added to the end of the item list
Debug "New combobox item added: " + a$
Else
Debug "Combobox item with this text is already existing: " + a$
EndIf
Else
Debug "Combobox item no. " + state + " selected, showing following text: " + a$
EndIf
EndSelect
EndSelect
Until Quit = 1
End