While It Only Takes One Line of Code to Update the INI Data File, You Must Take Into Account the INI File’s Encoding. Plus, DIY Implementation of the Other Cool ListView Features
By updating the data table file with the edits made via LegalListView.ahk, we save any changes for future use. Reloading the script will display the same recently updated window. This only requires one line of code:
LV_EditButtonSubmit: Gui,LV_Edit:Submit Gui, LegalList:Default LV_Modify(Row, , , ShortDef, Latin,LongDef) IniWrite, % ShortDef . "|" . Latin . "|" . LongDef , LegalInput.ini, InputKey, %HotString% Return
The IniWrite command above uses the forced expression operator % to directly update the INI file with the concatenated variables. Since the IniWrite command does all the updating and file writing, we don’t need extra code for saving the data.
This illustrates one advantage to using an INI file for the data table. If we saved the data in any other format, we would need to completely rewrite the file by piecing together three file parts and use the FileAppend command to overwrite the original file.
- First, we must write all of the data which precedes the change.
- Next, we append the changed data.
- Last, we add the remainder of the file.
When using an INI file, AutoHotkey completes these same steps—although, we don’t see or need to account for the different file pieces. (The ToDoList.ahk script saves the data file by completely overwriting the original file by sweeping through the ListView control while reading the text from each row. This automatically incorporates the three-step process by rewriting everything.)
Warning: UTF-8 Encoding Does Not Work for the INI Data File
When I initially tested the above code, I had saved the LegalInput.ini file with UTF-8 encoding. Anything I changed in the ListView would add a new record rather than update the original row. The online documentation informed me that I needed to save the file in another format. Both ANSI and Unicode (found in the NotePad Save As… option) work. If you get this type of unusual behavior, check your INI file encoding.
Change the Text Field to a ReadOnly Edit Field
In anticipation of future features, I changed the Text field for the Hotstring key into an Edit field. I did this to make it easier to add new data and change Hotstring keys using the LV_Edit window:
Gui, LV_Edit:Add, Edit, vHotstring w50 ReadOnly Gui, LV_Edit:Add, Edit, vShortDef w200 Gui, LV_Edit:Add, Edit, vLatin w200 Gui, LV_Edit:Add, Edit, vLongDef r3 w200 Gui, LV_Edit:Add, Button, , Submit Gui, LV_Edit:+OwnerLegalList
I want the Hotstring data to appear in the GUI, but I don’t want it immediately editable (the original reason for using a Text field). Therefore I added the ReadOnly option to the command making it inaccessible. Later, when adding new data, I’ll make the field editable by including the command:
GuiControl, LV_Edit: -readonly , Hotstring ; line continuation
The LegalListView.ahk script needs much more work but I must leave that to the users. Examples of the additional features appear in most of the other ListView apps which I discuss in my book AutoHotkey Applications. However, I list those features here and offer the AutoHotkey considerations.
More LegalListView.ahk Features
1. Adding New Records
Now that you have a ListView editing GUI window (LV_Edit) available, you can use it to add new data to both the ListView and the INI data table file. Write a subroutine which defaults the LV_Edit GUI to all blank fields (including an editable Hotstring key field as described above).
After adding data to the GUI, the new Submit routine requires a couple of new steps to ensure unique Hotstring key values:
- Before adding the new data, search the ListView for any keys matching the new Hotstring key. (Use a ListView search similar to that found in “Use the ListView GUI Control to Find Duplicate Entries in Data Table Files (AutoHotkey Legal ListView Part 2).”) If found, display a message insisting upon the creation of a unique Hotstring key, then Return to the editing GUI without making changes. If you do this check and Return before execution of the Gui, Submit command the editing window remains open and unchanged.
- If not found, use the LV_Insert() function to add the new item to the ListView and IniWrite to add the record to the file—including the new Hotstring key.
2. Deleting Records
Tip: Any time you delete data you should add a trap just in case you don’t actually want to delete it—”Are you sure you want to permanently delete this item?” Otherwise, the record can disappear before you realized that you selected the wrong menu item or hit the wrong Hotkey combination. (You can add automatic file backup whenever you launch the script by including the FileCopy command. If you want unique backup files, include the date stamp (%A_Now%) in the name of the file.)
3. Changing Hotstring Values
The steps for changing Hotstring key values in the INI file requires two steps:
- Add the new record.
- Delete the old record.
Caution: Remember, if duplicate key values appear in the INI file, the INI commands only finds the first occurrence in the file. However, when AutoHotkey inserts the records into the sorted ListView, they appear in the reverse order of appearance in the file. That means you must start with the last duplicate in the ListView to access the first matching record in the file. Anything you do to preceding ListView rows will overwrite the first matching record in the INI file—which corresponds to the last duplicate Hotstring key in the ListView.
First, I’ll discuss how to change Hotstring keys, then I’ll explain how to fix duplicate keys in the file using the same routine.
Changing Hotstring Key Values in ListView and the INI File
The LV_Modify() easily updates the ListView:
LV_Modify(Row, , Hotstring, ShortDef, Latin,LongDef)
Changing a Hotstring key value in the INI file requires the following steps:
- Add the new Hotstring record using the IniWrite command. The new (hopefully unique) key does not exist in the file—i.e. AutoHotkey can’t find it in the ListView search previously discussed. Therefore, after checking for Hotstring key uniqueness, AutoHotkey adds the GUI contents as a new record.
- Now, we must delete the old record using the IniDelete command.
Changing Duplicate Hotstring Keys in the INI File
Once you write a routine to add new Hotstring INI keys, you can use it to (carefully) remove duplicates.
- After using the CTRL+ALT+Q find duplicate Hotkey, start with the last duplicate key shown in the ListView.
- Use the edit Hotstring key routine to change the key. If unique, AutoHotkey adds the new record with the new key, then deletes the original key record (the first occurrence in the INI file).
- If more duplicates, move up in the ListView and repeat. As long as you change the matching Hotstring keys in the ListView from the bottom up, you’ll repair all of the dups. If you attempt it out of order, you will lose data. (Backup your data file before you start.)
Removing duplicates (if you didn’t do it previously by directly editing the INI file) offers the most complicated task for LegalListView.ahk. However, after you clean up the file, your script should prevent the addition of duplicate keys by checking the list each time you add a new record.
Calling the New Editing/Deleting Routines
You can use any number of techniques for launching these subroutines. Rather than adding more Hotkeys, I favor a right-click context menu as shown below.
The right-click context menu uses the built-in Label name GuiContextMenu (in our case LegalListGuiContextMenu). First, we set up a Menu command structure:
Menu, MyContextMenu, Add, Edit, EditItem Menu, MyContextMenu, Add, New, NewItem Menu, MyContextMenu, Add, Change Key, ChangeKey Menu, MyContextMenu, Add, Delete, DeleteItem
Next, we add the GuiContextMenu subroutine to display the menu;
LegalListGuiContextMenu: If A_GuiControl <> MyListView ; Display menu ListView clicks only. Return LV_GetText(EditText, A_EventInfo) ; Show the menu at the provided coordinates, ; A_GuiX and A_GuiY. These should be used ; because they provide correct coordinates ; even if the user pressed the Apps key. Menu, MyContextMenu, Show , %A_GuiX%, %A_GuiY% Return
Each Label name called in the Menu structure must exist within the script before it will load. Place the appropriate subroutines at the corresponding Label locations enclosing each with the Return command.
While I could have completed all of these steps for you, that would have ruined all the fun. Play with the script to see if you can get all of these things working for yourself. If you get confused, I’ve written about these topics extensively and you can find working examples at the ComputorEdge AutoHotkey Free Script page as listed below. If you have a particular problem, feel free to contact me.
Other ListView Examples
I’ve used the ListView control a number of times in the past and offer numerous examples at the ComputorEdge Free AutoHotkey Scripts page:
- 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 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 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.
- 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.
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.)