by Neville Franks - Soft As It Gets
Dear Reader, you may recall from last month's erudite issue that a certain Mr. Mark T. threw down the gauntlet about no one writing C articles for this magazine. Well it looks like I was laying in the wrong spot and the gauntlet hit me - ouch!! Truth be known we C/C++ programmers are far too busy writing "serious" code to have time to write magazine articles. Now putting that to one side, let me get to the nitty gritty.
Much is written these days about custom controls, windows sub and superclassing, edit control validation, virtual list boxes and the like. However there's one control which you don't see too much about, and that's the ComboBox.
I'm sure you've all seen the "List Files of Type" Combobox in the "File|Open" common dialog. If not open up Write in the Program Manager Accessories group and select Open from the File menu. The "List Files of Type" Combobox is great as far as it goes, but it doesn't allow the user to add their own "types", or modify or delete existing ones.
Some Windows applications like CodeWright do provide this capability, but not integrated into the File|Open dialog and they require you to enter items in side by side list boxes which isn't all that intuitive. I wanted to add user configurable file types to the next release of our programmer's editor, ED for Windows and decided to try and find a better solution to what I've seen to date.
The solution I came up with was to use a Combobox with an edit control and add the extra functionality needed to add, modify nd delete items. In this article I'll show you how you can give users the ability to delete items from the combobox list and how you can provide them with a method of adding new entries.
Unfortunately the user interface issues for this type of functionality are
not mentioned in "The Windows Interface, An Application Design Guide" by
Microsoft Press (not in my copy anyway), nor is it mentioned in any
other publication I've come across.
There are two user interface issues to resolve, what keys to use for the delete and add functions and what happens to new text entered in the edit control when the combobox loses focus.
Let's start with the easy one, deleting items. For this I decided on Alt+Delete. I've seen applications which use Delete to remove items when the list box is dropped down and then use the same key to delete text from the edit control when the list isn't dropped down - somewhat confusing in my opinion. By using Alt+Delete I can provide an unambiguous delete capability whether the list box is dropped down or not.
The second and more difficult problem is the interface to add new combobox items. Once some text is typed into the combo's edit control what action signifies it's time to add it to the list?
For example if you press Cancel or Escape you don't want it added to the list, right! And what about pressing Tab or Back Tab to move to the next or previous control - should it be added or not?
A further complication with this is that from the programming point of view it's not easy (read very difficult) to know what key or action caused the combobox to loose focus. All that Windows tells us is that a control is loosing focus - end of story.
In my opinion the best solution is to have a specific key that says "add me to the list". Again this is unambiguous and easy to use. So what key? You could pick a function key but which one? Or maybe the Insert key, it's not used for anything which is strange as you would expect it to toggle between overtype and insert modes. In the end the key I decided was most natural and hence settled on was Return or Enter. Now this caused a problem in its own right as Return is used to transfer focus to the dialog's default button, which is not what I wanted. It took a while to solve this problem and all will be revealed later on.
Ok, now we've decided on the user interface, let's get down to
how we implement the code to make it all happen.