Page 1 of 1

[SOLVED] (end)User Friendly Way to delete ListIconGadget line?

Posted: Thu Oct 16, 2025 4:50 am
by Randy Walker
I have a populated listIconGadget() Where I would like to allow the user to select a line and press Delete to remove that line. Is there any way to do that without a callback? (What a Pain!) We can use the following for sample purposes:

Code: Select all

 If OpenWindow(0, 100, 100, 300, 220, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   ListIconGadget(0, 5, 5, 290, 200, "Name", 100, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
   AddGadgetColumn(0, 1, "Address", 250)
   AddGadgetItem(0, -1, "Harry Rannit"+Chr(10)+"12 Parliament Way, Battle Street, By the Bay")
   AddGadgetItem(0, -1, "Ginger Brokeit"+Chr(10)+"130 PureBasic Road, BigTown, CodeCity")
   AddGadgetItem(0, -1, "Harry Rannit"+Chr(10)+"12 Parliament Way, Battle Street, By the Bay")
   AddGadgetItem(0, -1, "Ginger Brokeit"+Chr(10)+"130 PureBasic Road, BigTown, CodeCity")
   AddGadgetItem(0, -1, "Harry Rannit"+Chr(10)+"12 Parliament Way, Battle Street, By the Bay")
   AddGadgetItem(0, -1, "Ginger Brokeit"+Chr(10)+"130 PureBasic Road, BigTown, CodeCity")
   Repeat
     Select WaitWindowEvent()
       Case #WM_KEYDOWN
         Debug "KEY down"
         
         If GetKeyState_(#VK_DELETE)
           Debug "Yep!!!"
              h.i = CountGadgetItems(0)
              Debug h
              If h > 3
                h = h-1
                For e.i = h To 0 Step -1
                  If GetGadgetItemState(0, e)
                    RemoveGadgetItem(0, e)
                  EndIf
                Next e
              EndIf
         EndIf
     EndSelect
   Until Event = #PB_Event_CloseWindow
I figured out one way using combo of #WM_KEYDOWN and GetKeyState_(#VK_DELETE) as illustrated in the revised code above.

Re: (end)User Friendly Way to delete ListIconGadget line?

Posted: Thu Oct 16, 2025 5:23 am
by BarryG
For Windows you can do it like below. Change #WM_KEYDOWN to #WM_KEYUP if you don't want the user to hold down the Delete key.

Code: Select all

If OpenWindow(0, 100, 100, 300, 300, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ListIconGadget(0, 5, 5, 290, 290, "Number", 100, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
  For i=1 To 100
    AddGadgetItem(0, -1, Str(i))
  Next
  Repeat
    Event = WaitWindowEvent()
    If Event = #WM_KEYDOWN And EventwParam() = #VK_DELETE
      i = GetGadgetState(0)
      RemoveGadgetItem(0, i)
      SetGadgetState(0, i)
    EndIf
  Until Event = #PB_Event_CloseWindow
EndIf

Re: (end)User Friendly Way to delete ListIconGadget line?

Posted: Thu Oct 16, 2025 5:47 am
by Randy Walker
BarryG wrote: Thu Oct 16, 2025 5:23 am For Windows you can do it like below. Change #WM_KEYDOWN to #WM_KEYUP if you don't want the user to hold down the Delete key.
Ok, Your way is much cleaner than the method I figured out above. Thanks BarryG

Re: [SOLVED] (end)User Friendly Way to delete ListIconGadget line?

Posted: Thu Oct 16, 2025 8:12 am
by Mindphazer
The same code, but cross platform :

Code: Select all

Enumeration 
  #KeyDelete
EndEnumeration

If OpenWindow(0, 100, 100, 300, 300, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  AddKeyboardShortcut(0, #PB_Shortcut_Delete, #KeyDelete)
  ListIconGadget(0, 5, 5, 290, 290, "Number", 100, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
  For i=1 To 100
    AddGadgetItem(0, -1, Str(i))
  Next
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_Menu And EventMenu() = #KeyDelete
      i = GetGadgetState(0)
      RemoveGadgetItem(0, i)
      SetGadgetState(0, i)
    EndIf
  Until Event = #PB_Event_CloseWindow
EndIf

Re: [SOLVED] (end)User Friendly Way to delete ListIconGadget line?

Posted: Thu Oct 16, 2025 12:08 pm
by BarryG
Mindphazer wrote: Thu Oct 16, 2025 8:12 amAddKeyboardShortcut(0, #PB_Shortcut_Delete, #KeyDelete)
But then you can't use the Delete key in your app, such as when using a StringGadget.

Re: [SOLVED] (end)User Friendly Way to delete ListIconGadget line?

Posted: Thu Oct 16, 2025 12:25 pm
by spikey
BarryG wrote: Thu Oct 16, 2025 12:08 pm But then you can't use the Delete key in your app, such as when using a StringGadget.
You can chop and change shortcuts at will:

Code: Select all

Enumeration 
  #KeyDelete
EndEnumeration

If OpenWindow(0, 100, 100, 300, 500, "ListIcon Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ListIconGadget(0, 5, 5, 290, 290, "Number", 100, #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
  For i=1 To 100
    AddGadgetItem(0, -1, Str(i))
  Next
  StringGadget(1, 5, 305, 290, 25, "Edit me!")
  EditorGadget(2, 5, 335, 290, 100)
  SetGadgetText(2, "And me too!")
  
  Repeat
    Event = WaitWindowEvent()
    If GetActiveGadget() = 0
      AddKeyboardShortcut(0, #PB_Shortcut_Delete, #KeyDelete)
    Else
      RemoveKeyboardShortcut(0, #PB_Shortcut_Delete)
    EndIf
    If Event = #PB_Event_Menu And EventMenu() = #KeyDelete
      i = GetGadgetState(0)
      If i > -1
        RemoveGadgetItem(0, i)
        SetGadgetState(0, i)
      EndIf
    EndIf
  Until Event = #PB_Event_CloseWindow
EndIf

Re: [SOLVED] (end)User Friendly Way to delete ListIconGadget line?

Posted: Thu Oct 16, 2025 1:03 pm
by AZJIO
First, make sure that the item is selected.

Code: Select all

i = GetGadgetState(0)
If i > -1

Re: [SOLVED] (end)User Friendly Way to delete ListIconGadget line?

Posted: Thu Oct 16, 2025 1:07 pm
by Mindphazer
@spikey
Very nice !

Re: [SOLVED] (end)User Friendly Way to delete ListIconGadget line?

Posted: Thu Oct 16, 2025 1:13 pm
by spikey
AZJIO wrote: Thu Oct 16, 2025 1:03 pm First, make sure that the item is selected.

Code: Select all

i = GetGadgetState(0)
If i > -1
That's a good point. There's a potential runtime error there, if you don't.
Mindphazer wrote: Thu Oct 16, 2025 1:07 pm @spikey
Very nice !
:) Thank you!

Re: [SOLVED] (end)User Friendly Way to delete ListIconGadget line?

Posted: Thu Oct 16, 2025 7:48 pm
by Randy Walker
Mindphazer wrote: Thu Oct 16, 2025 8:12 am The same code, but cross platform :
Tested on Win 11 and works great. Thank you for the contributuion Mindphaser!!! Fred should add that snippet to the Help doc.

Re: [SOLVED] (end)User Friendly Way to delete ListIconGadget line?

Posted: Thu Oct 16, 2025 8:14 pm
by Randy Walker
Mindphazer wrote: Thu Oct 16, 2025 1:07 pm @spikey
Very nice !
I double that. OKay then... Fred should put spikey's snippet into the Help doc under RemoveGadgetItem since there is no sample code there now. Yeah Yeah!! That would be great! Thanks Fred!!!