Jump to content

Rectangular Selection in ListView


Recommended Posts

Multiple selections
Creation of multiple rectangle selections can be implemented as multiple iterations of creating a single rectangle selection. And in addition maintain information to keep track of these multiple selections.

In the listview, both single and multiple rectangle selections are visually illustrated as groups of colored cells. The colors are drawn through custom draw code. But the custom draw code requires a different technique for multiple selections than for single selections.

For a single rectangle selection the rectangle can be specified as item/subitem (row/column) coordinates (indexes) of two opposite corner cells in the rectangle. In the custom draw code, it can be checked whether a listview cell is located inside this rectangle. In that case, the cell can be drawn e.g. with a blue background color.

However, this method will not work for a large number of multiple selection rectangles because it'll take too long to check if a listview cell belongs to a single rectangle out of a large number of rectangles. A method that is independent of the number of rectangles is required.

One such method is to store the selections in an array with the same number of rows and columns as the listview. The value of each array element directly indicates the background selection color of the corresponding listview cell. To fill in background color in all cells on a listview page with 40 rows and 15 columns will require 600 array element lookups. In AutoIt, array element lookups are very fast. The rule of thumb is that you can look up 1000 integer values in an array in one millisecond. Filling background color in all 600 cells on the listview page will only take a few milliseconds. And that's fast enough. And the speed is completely independent of the number of selection rectangles stored in the array.

This is the Virtual-MRS.au3 example. The example demonstrates the use of the pure UDF. The rectangle selections are not used in user code. Run the example in SciTE with F5. Right-click listview and select "Multiple rectangle selections".


Use the following procedure to create the two selections shown in the image:

  • Click and drag the selection from (2,1) to (13,7).
  • Click and drag the selection from (4,2) to (11,6).
    Right-click in this inner selection and delete it.

If you have regretted deleting the inner selection:

  • Right-click one of the blue cells in the outer selection and select "Update saved selection".

If you have updated the outer selection so that you have a completely blue rectangle, repeat the steps to delete the inner rectangle.

Complete the selections in the image:

  • Click and drag the selection from (6,3) to (9,5).

The selection from (6.3) to (9.5) is the current selection and is drawn in a cyan color with the yellow active cell in the corner. Click a white cell to save the current selection after which it appears as a blue rectangle. The blue rectangle is now stored in the array discussed above. This array is named $MRS_aSubItems in code.

If you want to use a single cell as a rectangle selection, double-click the cell or press Enter key if you are using the keyboard. A single cell selection is not shown in the image.

You can right-click any cell in the listview. The items that appear in the menu depends on the context of the cell.

If you right-click the outer blue rectangle in the image above, you'll get a menu containing this rectangle and a corresponding submenu.

If you right-click the inner blue rectangle, you'll get a menu that contains both rectangles and corresponding submenus.

If you right-click the middle white rectangle, you'll get the menu that was created in user code to switch between UDF code and user code.

If you right-click the yellow active cell when it's not part of a selection rectangle, as shown in the image above, the menu will contain all rectangle selections and corresponding submenus.

If you right-click the current selection (cyan background color with the yellow active cell in the corner), you'll only be able to delete the selection.

The submenu always contains three menu items:

Make current selection is used to display the selection with the cyan background color and the yellow active cell in the corner. If you've made a lot of selections inside each other and on top of each other, it can be difficult to keep track of a specific selection. Use this submenu to show which group of cells belong to a specific selection rectangle.

Delete saved selection removes the corresponding selection rectangle after which the cells are displayed with the default white background color.

Update saved selection is used to recreate a partially deleted selection rectangle. This is demonstrated in the example above.

UDF/User code
When you right-click a white cell, you can switch between UDF and user code.

Switching from UDF code to user code preserves detail information about the individual selection rectangles. This is necessary to apply the selections in the user code. This is demonstrated in the examples below.

However, switching from user code to UDF code deletes all detail information about the individual selection rectangles.

In both situations, however, the selection rectangles are properly visually illustrated in the listview through cell background colors. The cell colors are drawn via selection rectangles stored in the $MRS_aSubItems array discussed above. These rectangles will only be deleted if you delete them manually. However, detail information about the selection rectangles is stored in $MRS_aSelections and other variables. Only these last variables are deleted when switching from user code to UDF code.

The code
Multiple Rectangle Selections (MRS) is implemented as a UDF in Includes\GuiListViewMRS.au3. GuiListViewMRS.au3 was started as a direct copy of GuiListViewSRS.au3. The central message handlers are MRS_GuiHandler() and MRS_ListViewHandler(). In MRS_ListViewHandler(), a large amount of code has been added to keep track of multiple selections. And a large amount of code has been added to handle right-click menus through $WM_RBUTTONDOWN messages.

Run examples in SciTE with F5.

Virtual-MRS.au3 is the example used above.

Virtual-MRS-Ex1.au3 is a very simple example of using the selection rectangles in the user code. They are used to draw corresponding rectangles in the user code. Once the script has started, right-click the listview and select "Multiple rectangle selections" to run the UDF code. Create a handful of random rectangle selections. Right-click a white cell and select "Default row selection" to return to the user code. Now you can see the rectangle selections in the user code.

Switch back to the UDF code. You can still see the rectangle selections. However, if you right-click one of the blue cells, there is no detail information about the rectangles to generate the usual rectangle selection menu. This detail information is lost by switching back and forth between user code and UDF code.

But there are still many different options. You can create a new rectangle selection exactly on top of an old one. This recreates the lost information. You can create a new rectangle selection exactly on top of an old one, and then delete both the new and the old selection. You can create a new large rectangle selection that covers all the old ones, and then delete both the new and all the old ones at once. And you can create brand new rectangle selections in addition to all the old ones.

Create a handful of random rectangle selections in the UDF code and return to user code. Now a small GUI appears where you can set the background color in the selections. The two ComboBoxes are made using functions in Implementing ComboBoxes. Add colors to the selections. Switch to UDF code to see what happens. Switch back to user code.

But there is a problem. If you select a black or dark color for a selection, you'll not see the text in the cells of the rectangle selection. Because the text is always drawn in black.

Virtual-MRS-Ex3.au3 solves the problem that texts is always drawn in black in rectangle selections.

Over the next weeks and months, more examples will be added to show how to use both the SRS and MRS UDFs.

The 7z-file at bottom of first post is updated with the new UDF and new examples.

You need AutoIt 3.3.12 or later. Tested on Windows 7 and Windows 10.

Comments are welcome. Let me know if there are any issues.

Edited by LarsJ
Post completed
Link to comment
Share on other sites

Link to comment
Share on other sites

Damn again. Such a nice article .... 
I don't know whether to go to sleep today because I probably wouldn't fall asleep anyway, without reading, analyzing, understanding, my brain would just explode.
This is so exciting.


Thanks will try to read your article next few days, I hope I will be able to understand all, just at first reading.


Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *


My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

In the first version of the code there were a few errors.

In the $WM_RBUTTONDOWN section of GuiListViewMRS.au3 that handles right-click, there are some right-click events that are simply not included.

In the Virtual-MRS-Ex3.au3 example, a recalculation of the $MRS_aSubItems array is performed. However, the recalculation isn't performed in all cases.

Both situations result in array subscript/dimension errors with subsequent crash of the code.

I've made a quick update to correct these errors.

I've also added a new example, Conventional-MRS-Ex3.au3, which is a copy of Virtual-MRS-Ex3.au3 but implemented as a conventional listview.

7z-file at bottom of first post is updated.

Edited by LarsJ
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...