ListView GUI Control for Viewing Data Table Files (AutoHotkey Legal ListView Part 1)

The Powerful ListView GUI (Graphical User Interface) Control Offers Advanced Features for Reading and Sorting Data Tables, Plus How to Make a ListView GUI Control Resizable

Recently, I wrote about how to use a data table to create Hotstrings using the Input command, then, after discovering that I can’t remember all the Hotstring combinations, showed how to build lookup menus from the same data table. Later, I used the same menu technique to demonstrate how to insert Latin legal terms in italics. I made a promise to create a Legal Lingo ListView GUI (Graphical User Interface) pop-up but, before I could undertake the task, other topics intervened. The time has come for me to deliver on my commitment.

Library Benefits

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.

You can find the original LegalInput.ini data table file at the ComputorEdge AutoHotkey Free Scripts site.

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.

I’ve used the ListView control a number of times in the past and offer numerous examples at the ComputorEdge Free AutoHotkey Scripts page:

  1. 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.
  2.  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.
  3. ToDoList
    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.

  4. The ImageList.ahk script (first mentioned in Chapter Twenty-four of my AutoHotkey Applications e-book) uses a simple two-column (one containing images) ListView control for exploring icons embedded in any Windows file.
  5. 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.
  6. 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.

Legal ListView Window
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.

The ListView GUI control includes a set of built-in functions for manipulating the rows and columns while extracting data. After setting up the ListView control with the Gui, Add, ListView command, AutoHotkey uses the LV_Add( ) function to insert new rows into the controls:

Gui, LegalList:+Resize 
Gui, LegalList:Default

; Create ListView control

Gui, LegalList:Add, ListView, Sort r20 w800 vMyListView gMyListview
  , Hotstring | Short Technical Definition | Legal Latin Terminology 
  | Long Definition
Loop, read, LegalInput.ini
  {
    LegalLingo := StrSplit(A_LoopReadLine , "=")
    If LegalLingo[1] = "[InputKey]"
      Continue

    LegalDef := StrSplit(LegalLingo[2] , "|")

; Function to insert new row into ListView
    LV_Add("", LegalLingo[1], LegalDef[1], LegalDef[2],LegalDef[3])
  }
Gui, LegalList:Show

Return

; MyListView label for future use
MyListView:
; No action yet!
Return

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 ListView LV_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 (GuiCloseGuiEscape, 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:

LegalListGuiSize:
  Width:=A_GuiWidth-20
  Height:=A_GuiHeight-20
  GuiControl, Move, MyListView, w%width% h%height%
Return

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.

jack

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.)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s