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

Just starting out? Need help? Post your questions and find answers here.
Randy Walker
Addict
Addict
Posts: 1101
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

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

Post 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.
Last edited by Randy Walker on Thu Oct 16, 2025 5:36 am, edited 1 time in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
BarryG
Addict
Addict
Posts: 4211
Joined: Thu Apr 18, 2019 8:17 am

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

Post 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
Randy Walker
Addict
Addict
Posts: 1101
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

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

Post 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
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 482
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

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

Post 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
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
BarryG
Addict
Addict
Posts: 4211
Joined: Thu Apr 18, 2019 8:17 am

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

Post 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.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

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

Post 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
Last edited by spikey on Thu Oct 16, 2025 1:12 pm, edited 1 time in total.
AZJIO
Addict
Addict
Posts: 2213
Joined: Sun May 14, 2017 1:48 am

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

Post by AZJIO »

First, make sure that the item is selected.

Code: Select all

i = GetGadgetState(0)
If i > -1
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 482
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

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

Post by Mindphazer »

@spikey
Very nice !
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

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

Post 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!
Randy Walker
Addict
Addict
Posts: 1101
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

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

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1101
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

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

Post 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!!!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Post Reply