[Class] LV_Rows - Copy, Cut, Paste and Drag ListViews (Updated Dec, 28, 2020)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Pulover
Posts: 612
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

[Class] LV_Rows - Copy, Cut, Paste and Drag ListViews (Updated Dec, 28, 2020)

04 Oct 2013, 07:22

This class is a collection of functions to allow additional features in ListViews that are not supported by AutoHotkey built-in functions.
It provides an easy way to add the commands Copy, Cut, Paste, DuplicateDelete, Move, Drag, Undo and Redo.

Features:
  • Edit functions: Copy, Cut, Paste, Duplicate, Delete, Move, Drag, CopyTo and MoveTo.
  • History functions: Add, Undo and Redo.
  • Group functions: EnableGroups, InsertGroup, RemoveGroup, InsertAtGroup, RemoveAtGroup, SetGroups, GetGroups, SetGroupCollapisable, RemoveAllGroups, CollapseAll and RefreshGroups.
  • Copy, cut and paste between different ListViews.
  • Keep history of individual ListViews to be used with Undo and Redo.
  • Drag-and-Drop shows a destination bar to indicate where selected rows will be moved to.
  • Auto-Scrolls ListViews when dragging above or below them.
  • Easily add and manage ListView Groups.
  • Supports Right-Click drag.
  • Supports Checked ListViews.
  • Supports ListViews with Icons.
  • Customize Auto-Scroll delay, thickness and color of destination bar.
Screenshots:
Spoiler
Notes:
  • They all operate on the active ListView control, like AHK built-in functions.
  • Edit functions can be called directly as easy as ^c::LV_Rows.Copy() (except CopyTo and MoveTo that need to be initialized with a HWND).
  • History and Group functions must be initialized via Handle := New LV_Rows().
  • To keep original icons you must initialize it passing the ListView's HWND, e.g. Handle := New LV_Rows(Hwnd).
  • The Drag function must be called from the LV G-Label when the Drag event occurs.
  • You can manage multiple listviews with the same Handle.
  • Pass the HWND's in inialization, e.g. Handle := New LV_Rows(Hwnd1, Hwnd2, Hwnd3...) and use SetHwnd() to select them.
  • Use SetHwnd() or InsertHwnd() to add more HWND's to the Handle.
:arrow: Sources, documentation and examples on GitHub

Special thanks to Micahs for the base code for the Drag function and to just me for the LV_EX library used for the Group functions.

See also TV_DragAndDrop
Last edited by Pulover on 02 Jan 2021, 08:11, edited 12 times in total.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
doomboy1000
Posts: 1
Joined: 24 Dec 2013, 19:20

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

24 Dec 2013, 19:31

Both this code (Dec 24, 2013) and the version hosted on the other forums seem to have some problems for me in AHK_L 1.1.10.01.

Specifically,
~Line 188:

Code: Select all

, DupRows.Paste(, False)
Yields an error about not allowing a blank parameter. Perhaps it should be

Code: Select all

, DupRows.Paste("", False)
Then, trying to drag a row yields an error, in which it seems the variable Line_W is never defined; the custom Gui Element "MarkLine" is given an unknown parameter "W H2 Y327 X278 NoActivate" (notice the W number is blank). I'm not sure where the root of this is happening, though.

Thanks for the great class!

EDIT: It seems the line

Code: Select all

LV_lw := LV_lw * 96 / A_ScreenDPI
Is causing a problem because A_ScreenDPI is not defined. Must this be done manually, or is this a feature of a newer AHK_L (or other fork)?
User avatar
Pulover
Posts: 612
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

24 Dec 2013, 19:42

Hi,
Please, update AutoHotkey to the latest version and try it again.

Thanks for the feedback.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
hoppfrosch
Posts: 443
Joined: 07 Oct 2013, 04:05
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

24 Apr 2014, 01:01

EDIT: Now it works as expected - strange .... Perhaps web wasn't very responsive at the time the problem occured ...

-----------------------

Hmmmmm ... cannot access the code you posted in your initial post.

Clicking on buttons "Spoiler -> SHOW", an "EXPAND" button occurs (which is unfamiliar, as "expand" is usually part of the Codebox header...) - clicking on the "EXPAND" button nothing happens (it toggles to "COLLAPSE-Button and vice versa)....
2014-04-24 07_56_33-[Class] LV_Rows - Copy, Cut, Paste and Drag ListViews • AHKScript.png
What it looks like ....
2014-04-24 07_56_33-[Class] LV_Rows - Copy, Cut, Paste and Drag ListViews • AHKScript.png (8.78 KiB) Viewed 16959 times
m3user
Posts: 235
Joined: 17 Jan 2014, 18:11

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

04 May 2014, 09:10

Is anything like this available for TreeView?
User avatar
Pulover
Posts: 612
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

04 May 2014, 09:21

Not yet, AFAIK.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Chef
Posts: 50
Joined: 14 Nov 2013, 13:01

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

20 Jun 2014, 04:01

Any reason why your not using LVM to do all the work? Using built-in functions for listviews makes it impossible to use.
User avatar
Pulover
Posts: 612
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

20 Jun 2014, 05:06

Chef wrote:Any reason why your not using LVM to do all the work?
Yes. I made this class based on functions and routines I had already for my app. It was easier and faster to use built-in functions, since they work pretty well.
Chef wrote:Using built-in functions for listviews makes it impossible to use.
I don't get your point. I can do everything I need with it.
If you want to create a version based on LVM, you're free to do it and post it or link it here, I don't have the time or interest to rebuild the class with it.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
think
Posts: 136
Joined: 09 Feb 2014, 05:20

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

16 Aug 2014, 09:20

Great class, thank you. Would it be possible to drag/drop items between tabs?
User avatar
Pulover
Posts: 612
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

16 Aug 2014, 09:43

think wrote:Great class, thank you. Would it be possible to drag/drop items between tabs?
Thanks. I've tried to do that for PMC but went into many issues... You can try to implement it yourself, I don't think you need to change anything in the class.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

17 Aug 2014, 02:55

Hello Pulover, works fine

Want ask you , how to manage a csv-file =

-Delete one/multiple selected row
-Modify
-Add-New
-Move-UP
-Move-Down
-Search and then delete or modify
-DoubleClick > run selected column (example c2)


example, see F1 ( csv-file )
( example reads variable e4x )

Code: Select all

#NoEnv
#SingleInstance, Force
#Include,Class_LV_Rows.ahk

SendMode Input                ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%
SetBatchLines -1

 F1=%a_scriptdir%\LV_Test21.txt

 e4x=
 (Ltrim Join`r`n
 Calc;calc.exe
 Notepad;notepad.exe
 ProgramFiles;%a_programfiles%
 )

ifnotexist,%f1%
  fileappend,%e4x%,%f1%


Gui,1: Default
Gui,1: Font,CDefault,Fixedsys


Menu, EditMenu, Add, Copy`tCtrl+C, Copy
Menu, EditMenu, Add, Cut`tCtrl+X, Cut
Menu, EditMenu, Add, Paste`tCtrl+V, Paste
Menu, EditMenu, Add, Delete`tDelete, Delete
Menu, EditMenu, Add
Menu, EditMenu, Add, Undo`tCtrl+Z, Undo
Menu, EditMenu, Add, Redo`tCtrl+Y, Redo
Menu, EditMenu, Add
Menu, EditMenu, Add, Move Up`tCtrl+Up, MoveUp
Menu, EditMenu, Add, Move Down`tCtrl+Down, MoveDown
Menu, MenuBar, Add, Edit, :EditMenu
Gui,1: Menu, MenuBar

Gui,1: Add , ListView, AltSubmit vLv1 gLVLabel x10 w320 r8 LV0x10000 backgroundgray +grid ,Name|Fullpath
; Create a handle for the History of first ListView.
HistoryLv1 := New LV_Rows()
HistoryLv1.Add()

LV_ModifyCol(1,200)
LV_ModifyCol(2,300)

gosub,fill
Gui,1: Show,x0 y0 h160 w350,TEST
return
;--------

Guiclose:
exitapp
;--------

fill:
Gui,1:submit,nohide
;fileread,e4x,%f1%                ;-  ????  if I read file how to manage .....
Loop, Parse,e4x, `n, `r
 {
 StringSplit,C, A_LoopField, `;
    LV_Add("",c1,c2)
 }
return
;--------

; Context Menu.
GuiContextMenu:
If !InStr(A_GuiControl, "LV")
   return
Menu, EditMenu, Show, %A_GuiX%, %A_GuiY%
return


; ListViews G-Label.
LVLabel:
Gui,1: ListView, %A_GuiControl%   ; Sets selected ListView as Default.
ActiveList := A_GuiControl        ; Sets a variable to define the active list for History entries.

; Detect Drag event.
If A_GuiEvent = D
{
LV_Rows.Drag()            ; Call Drag function.
History%ActiveList%.Add() ; Add an entry in History.
}
return

Copy:
LV_Rows.Copy()            ; Copy active List selected rows.
return

Cut:
LV_Rows.Cut()             ; Cut active List selected rows.
History%ActiveList%.Add() ; Add an entry in History.
return

Paste:
If (LV_Rows.Paste())      ; Paste contents from memory.
History%ActiveList%.Add() ; Add an entry in History if Paste had data.
return

Delete:
If (LV_Rows.Delete())     ; Deletes selected rows.
History%ActiveList%.Add() ; Add an entry in History if there are rows selected.
return

MoveUp:
If (LV_Rows.Move(1))      ; Move selected rows up.
History%ActiveList%.Add() ; Add an entry in History if there are rows selected.
return

MoveDown:
If (LV_Rows.Move())        ; Move selected rows down.
History%ActiveList%.Add()  ; Add an entry in History if there are rows selected.
return

Undo:
GuiControl,1: -Redraw, %ActiveList%
History%ActiveList%.Undo() ; Go to previous History entry.
GuiControl,1: +Redraw, %ActiveList%
return

Redo:
GuiControl,1: -Redraw, %ActiveList%
History%ActiveList%.Redo() ; Go to next History entry.
GuiControl,1: +Redraw, %ActiveList%
return
;=============================================================
think
Posts: 136
Joined: 09 Feb 2014, 05:20

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

10 Sep 2014, 14:58

Hi, I have one more question. In my script I use few ListViews in different tabs so I keep history of each ListView using different handles. Sometimes I move the content of one ListView to another. Is it possible to assign/move the history from one handle to another? Thanks.

Code: Select all

History1 := New LV_Rows()
History1.Add()
; ...
History2 := New LV_Rows()
History2.Add()
; ...
; how to assign History1 to History2?
User avatar
Pulover
Posts: 612
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

10 Sep 2014, 16:26

think wrote:Hi, I have one more question. In my script I use few ListViews in different tabs so I keep history of each ListView using different handles. Sometimes I move the content of one ListView to another. Is it possible to assign/move the history from one handle to another? Thanks.

Code: Select all

History1 := New LV_Rows()
History1.Add()
; ...
History2 := New LV_Rows()
History2.Add()
; ...
; how to assign History1 to History2?
Well, you could try History2 := History1. ;)
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
think
Posts: 136
Joined: 09 Feb 2014, 05:20

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

12 Sep 2014, 15:19

Of course... It didn't work at first as I had other issues... Thanks.
highend
Posts: 47
Joined: 24 Nov 2014, 16:57

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

20 Sep 2015, 11:15

Everything works just fine until... I create a new window with e.g. Gui, New, , testwindow
right before the Gui, Add, ListView line

When I do that, all the context menus stop working (they don't do anything anymore)

I can't find out what to change in the code to get it working again *sigh*

Any help would be appreciated :)
User avatar
Pulover
Posts: 612
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

16 Mar 2016, 19:58

Update:
  • Added support for multiple ListView hwnd's in the same handle.
  • Added ListView Groups functions (thanks to just me).
Now you can use the same Handle to manage different ListViews, keeping individual history but sharing copied data. The documentation can be found on GitHub too.
All original behavior was kept, so updating shouldn't break any script.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
gr81

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

16 Mar 2016, 20:39

Your class as is doesn't work on my side, too many errors, I had to go through the old version and change the built-in functions (that I needed) to LVM.
You need to get rid of all built-in functions and use LVM instead.
Thanks anyway.
User avatar
Pulover
Posts: 612
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: [Class] LV_Rows - Copy, Cut, Paste and Drag ListViews

16 Mar 2016, 21:03

gr81 wrote:Your class as is doesn't work on my side, too many errors
What errors?
gr81 wrote:You need to get rid of all built-in functions and use LVM instead.
It works for me so I'm not inclined to change that unless someone shows me a good reason.

Feel free to collaborate with the project.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Pulover
Posts: 612
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Update v1.1.1

29 Apr 2016, 11:11

Update v1.1.1
  • Fixed collapsed groups expanded after refreshing.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Pulover
Posts: 612
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Update v1.1.2

09 Jun 2016, 17:58

Update v1.1.2
  • Removed *CopyTo* and *MoveTo* functions due to bugs with setting target ListView.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 113 guests