Find an item in a ComboBoxGadget by name?

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Find an item in a ComboBoxGadget by name?

Post 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!
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Find an item in a ComboBoxGadget by name?

Post 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()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Find an item in a ComboBoxGadget by name?

Post 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!
Post Reply