Page 1 of 1

Find an item in a ComboBoxGadget by name?

Posted: Wed Apr 23, 2025 10:02 pm
by Quin
In my app, I want to remove a particula item from a combo box. The only piece of info I'll have about what I need to remove is the string of the item, not the index itself. How can I find the index of the item to remove?
For example:
Combo box contains string1, string2, string3, and string4. My selection is on string1, and an event is posted saying to remove string3 from the combo box. What's the best way to find the index of string3, so I can then call RemoveGadgetItem with that index?
Thanks!

Re: Find an item in a ComboBoxGadget by name?

Posted: Wed Apr 23, 2025 10:29 pm
by mk-soft
... ;)

Code: Select all

;-TOP

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(0)
  dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
  ; Resize Gadgets
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(0, #PB_Ignore, #PB_Ignore, 600, 400, "Test Window", #WinStyle)
    ; MenuBar
    CreateMenu(0, WindowID(0))
    MenuTitle("&File")
    MenuItem(99, "E&xit")
    
    ; StatusBar
    CreateStatusBar(0, WindowID(0))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(0)
    dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
    ComboBoxGadget(0, 10, 10, 120, 25)
    ; add items
    For i = 1 To 4
      AddGadgetItem(0, -1, "Item " + i)
    Next
    
    ; remove item 3
    cnt = CountGadgetItems(0) - 1
    For i = 0 To cnt
      If GetGadgetItemText(0, i) = "Item 3"
        RemoveGadgetItem(0, i)
        Break
      EndIf
    Next
      
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), 0)
    
    ; Main Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case 0
              Break
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            Case 99
              PostEvent(#PB_Event_CloseWindow, 0, 0)
              
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()

Re: Find an item in a ComboBoxGadget by name?

Posted: Wed Apr 23, 2025 10:50 pm
by Quin
Thanks mk-soft, I figured this was probably stupid simple and my brain just wasn't giving me the answer. This works perfectly!