Added: You can now drag items bewteen ListViews as long as they have the same number of columns and neither have checkboxes. Thanks to mesale0077 for suggesting it and GreenCan for all the testing.
Changed: Much of the internal UDF code has been rewritten as a result of the above.
New UDF and examples in the zip below.
Warning: There were major script-breaking changes in the release of 19 Apr 13 from previous versions of the UDF!!!
Please read the change list for that release in the spoiler below if you have not already done so.
It seemed that I wanted to add, delete, edit or move items in a ListView quite often in my scripts and I got fed up with having to rewrite the code to do it each time. I also wanted to be able to drag items with the mouse. So I decided to write a UDF to make life easier and here is the result - GUIListViewEx.
If you are interested in how it works, then read this bit - if not, then skip over it:
The UDF is pretty easy to use:
- You start by creating a ListView (either native or UDF) and passing the returned ControlID/handle and the array you used to fill it to the _Init function of the UDF. You also indicate whether the array has a count in the  (or ) element and if you create an empty ListView, the UDF will still cope and will shadow any items that you insert later. If you have a ListView filled with data but no matching array, there is a function to read that data into an array for you. You can select a colour for the insert mark when dragging items if you are going to use this feature - the default is black - and decide whether to have a shadow of the dragged item follow the mouse. Finally you can set the ListView to be sortable and editable - with various options to determine how the editing process works.
- You need to register a few Windows messages, but this is a single call to the _MsgRegister function. If you already have handlers for the relevant messages, there are functions to call within these handlers instead. If you do not want to drag, then you only need the WM_NOTIFY handler loaded.
- Then you just need to call the main _Insert($vData), _Delete, _Up, and _Down functions when the appropriate button is pressed, select and drag items, or use one of the edit functions and your ListView responds automatically.
- The UDF shadows the contents of the ListView (as explained in the spoiler section above) so you can get its current state at any time with the _ReturnArray function - for example if you wanted to save the data within. Many of the functions actually return this data after each call just to help you keep track.
- When you have finished with the ListView, you should use the _Close function to clear the memory used by the UDF to shadow its contents. It is not vital, but if you use a lot of ListViews and do not do this, you could end up running out of memory.
- You can have as many ListViews as you wish on display at any one time and the same "Insert", "Delete", "Up" and "Down" buttons can be used for them all - you just have to click on the one you want to be active. The UDF also allows you to set the active ListView programatically (_SetActive) - and to determine which is currently active (_GetActive).
There are 3 example scripts to show the UDF working on native and UDF created ListViews, with single or multiple columns, and either filled or empty along with the UDF itself in this zip file:
Credit to martin (for the basic drag code which I found on the forum) and the Array UDF authors (for the basis of the array functions). GUIListViewEx.zip 23.52K 66 downloads
Happy for any feedback - hopefully positive!
P.S. Once again the forum software is messing with one of the function names - it should read _GUIListViewEx_Edit+On+Click without the plus signs.
Edited by Melba23, 30 April 2013 - 01:27 PM.