Site icon CWL

Programming: The Elusive Listbox

From the very first days of designing applications with Delphi I have lent much of my interface enhancements to things I have seen while using Applications or even the big behemoth, Windows itself. One of the coolest interface element that evoked a “How do I do that?” out of me was the ListBox addition and removal of items that are later used in the program in some sort or another. I had always though making such great use of the ListBox required quite allot of programming. In this article I’ll show you how easy this is.

The key to making a ListBox (shown at left) useful is to add elements and meaning to the box itself. You may be using the ListBox to allow the user the list configuration files to look for when loading, or you may want to allow the user to define which log file to make use of when saving, or you may even be allowing the user to set a list of servers to use for a service. Using two (or more) buttons to control the adding an removing of the entries in this box give your application a more professional look.

The ‘Add’ and ‘Del’ Buttons

I’ll start with the two buttons that control the addition and removal of items in your list box. This will give the user control over what is shown. First I add the listbox (I’ll call it ListBox1) and then add two buttons (Call them Button1 and Button2 respectively). The code for the first button (the add button) will look like this:

procedure TForm1.Button1Click(Sender: TObject);
var
newfile : string;
begin
newfile := InputBox('Question', 'Please Enter a file Name', '');
ListBox1.Items.Add(newfile);
end;

In this procedure I’m using the InputBox() function to ask the user for a basic string. This string will represent a log filename in the above example. Once I have the string (it is placed in the newfile variable), then I can add the string to my ListBox1. Next, I work on the code for the Del button:

procedure TForm1.Button2Click(Sender: TObject);
begin
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;

This code does one thing, it deletes the currently selected item in ListBox1. This will move any items below it up automatically. If you would like to check for errors, you may want to check the value of Itemindex and incorporate that into an if statement and then use a ShowMessage() to tell the user he/she must select and item to proceed. Of course, you may want to jazz up the program by using your own button components or use a bitmap button from within Delphi – the choice is yours.

Other Cool ListBox Tricks

Right-Click Delete

One nifty feature to add for the users is to include a menu on right-click of the item in the listbox. To do this, add the PopUpMenu component (from the Standard toolbar) to your application’s form. Then, highlight your ListBox (ListBox1 in my case) and show the properties for this listbox (you can get to the properties after selecting the ListBox by pressing F11). Find the PopUpMenu item and select the PopupMenu you just added.

Once you have that done, select the PopupMenu component icon and right-click on it. Click on “Menu Designer…” to up the design window for your popup menu. You will have one item defined already – the only thing to change is the caption in your menuitem’s Caption property. Change this to Delete. You should notice the look of your menu item changes when your done changing the Caption.

To now make your menuitem do work, double-click on this item while still in the menu designer. You’ll be taken to the code for the onlick event of that menuitem. Add one line to the function and it should look like this:

procedure TForm1.Delete1Click(Sender: TObject);
begin
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;

You’ll notice the one line is exactly the same code as what was used in your “Del” button. Now, save and compile your project and test your new ListBox. You may also want to include the Add… function as part of your menu as well.

INI File (or registry) interaction

Taking this one setup further, your ListBox will be extremely useful if the information contained in it stays with your application and can be used after the program has been terminated and restarted. Adding this persistent information is done through the use of INI (pronounced innnn-eeee) files or through the use of the registry. I prefer to use INI files because they generally make your application more portable. In this example using the Registry to store the same information is almost exactly the same (you’ll use the regini unit instead of the inifiles unit).

To start, you need to be able to create INI files, this is enabled by using the inifiles unit. Place inifiles in your uses clause at the beginning of your unit1. Now that you can create INI files, I like to make sure that they are created using a convention of having the same name as the application and only the extension changes. This way, the INI file is much easier to find. An application with an executable named project1.exe will use the file project1.ini with this code:

You will need to initialize two variables in your Unit’s var section:

AppIni : Tinifile;
appininame : string;

Then, add the following lines of code to the beginning of your FormCreate procedure:

appininame := Application.ExeName;
appininame := StrLCopy(Pchar(appininame),Pchar(appininame),StrLen(Pchar(appininame))-3);
appininame := appininame +'INI';

appini := TIniFile.Create(appininame);

What does this do? The core part of this code truncates the string C:\PROJECT1.EXE to C:\PROJECT1. and then adds INI to the end of that string producing the location your INI file that will be read and used later.

So now that we have an ini file (named appini in my example) we can now read and write to this file to add items to our ListBox. I’ll let you decide the best place to put the writing of the ListBox’s contents to file (usually when the form closes or including a save settings button) and the best place to put the loading of the information from the INI file (usually in the FormCreate function somewhere). Here is the code to write and read:

Writing your ListBox to an INI file

appini.EraseSection('LogFiles');
for I := 0 to listBox1.Items.Count - 1 do
begin
appini.WriteString('Clients',inttostr(I), ListBox1.Items.Strings[I]);
end;

Reading your list from an INI file and placing it in a ListBox

appini.ReadSectionValues('LogFiles',CList);
for I := 0 to CList.Count - 1 do
begin
ListBox1.Items.Add(appini.readstring('Clients',inttostr(I),'Value not read'));
end;

You’ll notice the section I’ve used is LogFiles, you can use and section you like. Your INI file will contain a section that looks like this:

[LogFiles]
0=errlog.txt
1=mmlog.txt
2=nmlog.txt
3=log.txt

Conclusion

This article should give you enough power to implement listboxes to control settings that require user interaction and multiple options. Your application will be a more powerful tool for that. If you have any suggestions about articles you’d like to see or other interesting interface elements you might have previously seen and would like to have implemented but didn’t know how, let me know.

Exit mobile version