The purpose of opening a Legal Lingo ListView GUI is not so much to add more features, but to both find a convenient way to display and work with the data table file. Let’s face it, the current data table I provided (LegalInput.ini) is loaded with errors and duplicate INI search keys—both of which cause problems. By reading the data table into a Windows ListView GUI control, I can quickly view and sort the data while looking for errors. Some mistakes I’ll need to correct explicitly in the file, but many more I can change and manipulate by developing special functions for adding, editing, deleting, then saving entries in the ListView GUI. (That part comes later.)

In this series of blogs, I plan to introduce the innate features of the powerful ListView control then how to build-in additional capabilities. In this first blog, I introduce the integral components of the control and how to make it resizeable.

The ListView GUI Control

While probably the most complicated of the AutoHotkey GUI controls (similar to TreeView in complexity), ListView offers immense power and flexibility.

The ListView GUI control provides the ideal display for a data table. Composed of rows and columns, AutoHotkey can read in the data from any text file and display it in a format which looks similar to a spreadsheet. As part of the Windows ListView control, you can sort the table on any column by merely clicking its header, select any row or group of rows (CTRL+Click) with a click of the mouse, expand the column width or change the order of columns by dragging the edge of the column header or the header itself, respectively. Plus, you can easily make the ListView resizable with a short AutoHotkey routine found at the end of this blog.

The AddressBook.ahk script (discussed in my AutoHotkey Applications e-book) uses the ListView control as the primary interface for listing, editing and storing addresses. The script saves the ListView data to a file—reading the latest version when reloading the script.

The AutoHotkeyControl.ahk script (also discussed in my AutoHotkey Applications e-book) uses a simple ListView structure to build a control panel for your most-used AutoHotkey scripts or any other Windows application. It even demonstrates how to add context-sensitive menus to a ListView entry for access to specific app features.

The ToDoListColor.ahk script allows the user to add, edit, and delete tasks in an always-on-top GUI pop-up window.

The CalorieCount.ahk script (another product from my AutoHotkey Applications e-book) uses a ListView control for maintaining and saving a daily food consumption list. The script saves the ListView data to a file—reading the latest version when reloading the script.

The ToDoList.ahk script uses a ListView control to add, edit, display, and delete reminders. It employs a right-click context menu for Edit and Delete (see image above). The script saves the ListView data to a file—reading the latest version when reloading the script.

The ToDoListColor.ahk script adds color to individual rows of the ToDoList.ahk script when checking a task. (Note: I updated to the latest version of AutoHotkey 1.1 and this script no longer displays the color rows shown above. Go figure! Not sure what happened, but don’t have time to debug it now.)

Each of these scripts demonstrates various built-in functions and features of the ListView control. In this series, I plan to introduce many of the same techniques which I explain in AutoHotkey Applications for editing and maintaining the LegalInput.ini data table. But first, we read the data table into the ListView control.

After loading the LegalInput.ini data table into the ListView, I can sort the data in each column by clicking the heading, change the column widths (or order) using a left-mouse drag, or resize the window and ListView control (special code required) by dragging the bottom left corner. I can now identify errors in the data table by viewing the window. For example, three redundant Hotstring keys (athi) appear in the red box in the first column. The INI file ultimately must contain unique keys.

Merely adding the data to the window without attaching any extra features gives you a number of immediate benefits. After loading data into a ListView control, the sort option allows you to immediately see everything in alphabetical order based upon the first column of the control. As shown above, this makes it easy to recognize duplicate search keys in the INI file. You’ll find this critical for eliminating duplicate INI keys since AutoHotkey will only locate the first of the duplicates when searching an INI file.

Even though I plan to add data editing capabilities to the primary data fields of the ListView GUI window, it won’t work properly for the INI search keys. I must edit those in the original file since using an INI lookup stops at the first occurrence of a key. When changing the Hotstrings into unique keys, you must modify and save them in the original file, then reload the script to update the GUI window.

Tip: You can write an AutoHotkey routine which locates duplicate keys by scanning the first column of the ListView and comparing INI key values, but I’ll save that technique for next time.

How to Find Errors in the Data Table

As I noted above, for the future ListView tricks to work, the INI keys (the Hotstrings in the first column of the display) must contain unique data—no duplicates. When I first loaded the data into the file, I ignored this problem. By sorting the first column when loading the data, I can immediately identify the duplicate keys. First column alphanumeric sort allows you to visually locate repeated INI keys.

Plus, many of the other fields may contain blank data. By clicking on column headers, ListView sorts that column and places all the blank fields at the top. (Click the header again to reverse the sort.) Ultimately, I plan to add a pop-up editing window for directly updating the data. Plus, the same future editing window will allow the addition of more data entries, including inserting a new INI search key.

Tip: After sorting the first column in alphanumeric order, pressing any character on the keyboard jumps the display to the first matching row containing that initial character.

Loading Data into the ListView GUI Control

Similar to the Loops used in previous parts of this Legal Lingo series, AutoHotkey reads the data file line-by-line parsing the data into fields. The fact that we use an INI file for storing the data doesn’t affect this first step. Later, when updating the items in the file, the INI file format will make the process much easier.

Note: I gave the GUI window a unique name (LegalList) just in case I ever combine it with another script or plan to use more than one GUI in the same script (which I envision for this example when I add the pop-up editing window). However, when using any built-in events as shown below, I must precede the event label with the GUI name (e.g. GuiSize ⇒ LegalListGuiSize).

The Gui, Add, ListView command (in red above) includes the Sort option for placing the data in INI key order, the vMyListView option becomes the name of the ListView control and the gMyListView label name calls the subroutine MyListView which (for now) does nothing but eventually becomes an editing feature.

The script parses the file data in the same manner as demonstrated in the earlier blogs.

After skipping the first line of the INI file:

If LegalLingo[1] = "[InputKey]"
Continue

AutoHotkey reads and parses each line of data into its component parts. We then we use the ListViewLV_Add() function (in green above) to insert each line from the data table into the window control:

LV_Add("", LegalLingo[1], LegalDef[1], LegalDef[2],LegalDef[3])

Then, we display the window: Gui, LegalList:Show

We can now peruse the data while looking for errors. However, we must make all corrections directly to the INI text file—at least for now. We’ll soon add editing controls.

Resizing the ListView Control

Adding the Gui, LegalList:+Resize command to the script only allows for the resizing of the main window. As you drag the main window edge or corner, the main GUI does change, but the GUI controls inside do not adjust in size or shape. For that, you need to program the control to adapt as you resize the window.

GUI Window Events

You’ll find a number of built-in GUI window event labels (GuiClose, GuiEscape, GuiSize, GuiContextMenu, and GuiDropFiles) which AutoHotkey automatically recognizes whenever they appear in a script with. For controlling the dimensions of the ListView we use the GuiSize label in conjunction with the GuiControl, Move command and the built-in A_GuiWidth and A_GuiHeight variables:

MyListView identifies the target ListView control. As you drag the corner or edge of the GUI window, the GuiSize subroutine fires. The GuiControl command instantly resizes the ListView control relative to the new GUI window dimensions. Note: Since we named the GUI (LegalList), we must concatenate that name to the beginning of the window event label (LegalListGuiSize).

Next time, I’ll demonstrate how to write a routine for moving through the ListView rows looking for redundant INI search keys.

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)