Moving ListView Gadget Items
Posted: Thu Jun 08, 2023 4:19 pm
I've searched the forum and found there is scant information on this (perhaps I'm not asking correctly). But I have a form with a ListView gadget. Please see code:
In this example I'm just using filenames from the Windows directory to populate the ListView. (This will eventually be a playlist editor). On the right are buttons I would like to use to rearrange positions of the ListView items. Clear and Delete work fine. But I would like to highlight an item and press UP to move it up toward the top of the list and DOWN to move it lower. Is there a way of swapping index positions for items? Do you have to somehow redraw the ListView after it is updated? I apologize of this is beating a dead horse like how to detect if a file exists but I'm still kinda' new to PB. Any help would be appreciated. Thanks.
Code: Select all
Global Window_0, ListView_0, Button_1, Button_2, Button_3, Button_4, Button_5
Procedure OpenWindow_0(x = 0, y = 0, width = 560, height = 380)
Window_0 = OpenWindow(#PB_Any, x, y, width, height, "ListView Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListView_0 = ListViewGadget(#PB_Any, 10, 10, 370, 360)
Button_1 = ButtonGadget(#PB_Any, 410, 30, 120, 40, "Up")
Button_2 = ButtonGadget(#PB_Any, 410, 100, 120, 40, "Down")
Button_3 = ButtonGadget(#PB_Any, 410, 170, 120, 40, "Add")
Button_4 = ButtonGadget(#PB_Any, 410, 240, 120, 40, "Delete")
Button_5 = ButtonGadget(#PB_Any, 410, 310, 120, 40, "Clear")
EndProcedure
Procedure PopulateListview()
Directory$ = "C:\Windows\"
If ExamineDirectory(0, Directory$, "*.*")
While NextDirectoryEntry(0)
If DirectoryEntryType(0) = #PB_DirectoryEntry_File
AddGadgetItem(ListView_0, 0, DirectoryEntryName(0))
EndIf
Wend
FinishDirectory(0)
EndIf
EndProcedure
; Main ==========================================================================================================
OpenWindow_0()
PopulateListView()
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget
Select EventGadget()
Case Button_4
Index = GetGadgetState(ListView_0)
If Not Index = -1
RemoveGadgetItem(ListView_0, Index)
EndIf
Case Button_5
ClearGadgetItems(ListView_0)
EndSelect
EndIf
Until Event = #PB_Event_CloseWindow