Moving ListView Gadget Items

Just starting out? Need help? Post your questions and find answers here.
Catdaddy
User
User
Posts: 12
Joined: Wed Mar 23, 2022 3:40 pm

Moving ListView Gadget Items

Post by Catdaddy »

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:

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
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.
Fips
User
User
Posts: 28
Joined: Sun Feb 20, 2022 1:03 pm

Re: Moving ListView Gadget Items

Post by Fips »

Code: Select all

Global Window_0, ListView_0, Button_1, Button_2, Button_3, Button_4, Button_5

Procedure Switch_Lines(gad_num.i, line_1.i, line_2.i)
  Protected.s text_line_1, text_line_2
  
  text_line_1 = GetGadgetItemText(gad_num, line_1)
  text_line_2 = GetGadgetItemText(gad_num, line_2)
  
  
  SetGadgetItemText(gad_num, line_1, text_line_2)
  SetGadgetItemText(gad_num, line_2, text_line_1)
EndProcedure

Procedure MoveItemDown()
  Protected.i active_line, nb_entries
  
  active_line = GetGadgetState(ListView_0)
  nb_entries = CountGadgetItems(ListView_0)
  
  
  If active_line > -1 And nb_entries -1 > active_line
    Switch_Lines(ListView_0, active_line, active_line + 1)
    SetGadgetState(ListView_0, active_line + 1)
  EndIf
  
  
EndProcedure

Procedure MoveItemUp()
  Protected.i active_line, nb_entries
  
  active_line = GetGadgetState(ListView_0)
  nb_entries = CountGadgetItems(ListView_0)
  
  
  If active_line > 0
    Switch_Lines(ListView_0, active_line, active_line - 1)
    SetGadgetState(ListView_0, active_line - 1)
  EndIf
  
  
EndProcedure

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\"
  Directory$ = GetUserDirectory(#PB_Directory_Downloads)
	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)
			Case Button_2
			  MoveItemDown()
			Case Button_1
			  MoveItemUp()
		EndSelect
	EndIf
Until Event = #PB_Event_CloseWindow

RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Moving ListView Gadget Items

Post by RASHAD »

Hi

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 ListView_0
 		    Select EventType()
 		      Case #PB_EventType_LeftClick
 		        curitem = GetGadgetState(ListView_0)
 		        Debug curitem
 		        uptext$ = GetGadgetItemText(ListView_0,curitem-1)
 		        curtext$ = GetGadgetItemText(ListView_0,curitem)
 		        downtext$ = GetGadgetItemText(ListView_0,curitem+1) 
 		    EndSelect
 		    
			Case Button_1
  			If curitem > 0
          SetGadgetItemText(ListView_0,curitem-1,curtext$)
          SetGadgetItemText(ListView_0,curitem,uptext$)
        EndIf
        
      Case Button_2
        If curitem < (CountGadgetItems(ListView_0)-1)
          SetGadgetItemText(ListView_0,curitem+1,curtext$)
          SetGadgetItemText(ListView_0,curitem,downtext$)
        EndIf        
        
			Case Button_5
				ClearGadgetItems(ListView_0)
		EndSelect
	EndIf
Until Event = #PB_Event_CloseWindow

Edit : Added check for proper act
Last edited by RASHAD on Thu Jun 08, 2023 10:28 pm, edited 1 time in total.
Egypt my love
Catdaddy
User
User
Posts: 12
Joined: Wed Mar 23, 2022 3:40 pm

Re: Moving ListView Gadget Items

Post by Catdaddy »

Thank you Fips and Rashad. This is exactly what I needed. I come from AutoIt programming and am still getting used to the minor differences with PB.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Moving ListView Gadget Items

Post by Paul »

Better use Fips code as RASHAD doesn't check for problems if you try and move first item up or last item down. :oops:
Image Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Moving ListView Gadget Items

Post by RASHAD »

My previous post updated
Still the code is very simple and very direct to PB new users :)
Egypt my love
Post Reply