Download the source code (3.25 kb) for this article
For those of us who completely ditched ASP in favor of compiled VB COM objects
(see article), life has been
made a lot less aggravating. One side effect of the move away from ASP is the need to generate
various listboxes (the <select>...</select> variety) in code. Since most listboxes
are pretty much the same, it stands to reason that a reusable
class is needed that generates HTML listboxes quickly and easily. It further stands to reason that
since we all know and love the way the standard listbox works, why not emulate that interface.
So that's what this class does: it emulates the standard listbox interface, while adding its HTML-specific
methods and properties and improving on existing methods. Here is what's changed from the standard interface:
HtmlList = object.Render
Returns the generated HTML list. It is this easy.
object.AddItem Item as Variant, Optional Index as Long, Optional ItemData as Variant
As you can see, I've added ItemData parameter as optional, so that you can set a line
in one shot. Also, both Item and ItemData are now Variants so if you are adding
data from the register you no longer have to check whether it is NULL or not.
index = object.SearchItem Item as Variant
index = object.SearchItemData ItemData as Variant
I've added search routines looping through the list or reaching for that
ubiquotous SendMessage API. This routine returns an index of the found item or
-1 if nothing found. You can use it like this: oList.ListIndex = oList.SearchItem("Some Item")
Among other changes are properties like Name, Size, RenderMethod and Style that basically
define the look of the listbox. RenderMethod is important because it defines whether
the ListBox will have SELECT headers around the data or not.
Check out the attached sample linked at the top of this page.
Some of iteration of this handy class include methods where you can assign
it a Recordset and have it spit out a populated list. I didn't include it here
because this functionality is particular to my needs - yours maybe different. This
class has saved me countless hours. I hope it does the same for you.