How Can I Remove an Item from a List Box in an HTA?


Hey, Scripting Guy! Question

Hey, Scripting Guy! In an HTA, how can I remove an item from a list box?

— LW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, LW. You know, we Scripting Guys are forward-thinking guys and, as such, we never look back. Did we put an item in a list box? If so, there must have been a darn good reason for that and we’d never even consider removing it. What’s done is done, you know; we can’t spend all our time reliving past mistakes. (Trust us: it would take a long time for the Scripting Guys to relive all their past mistakes.)

Most people aren’t like the Scripting Guys, however. (Hey, who said “thank goodness”?) On top of that, there are plenty of valid reasons for wanting to remove options from a list box. For example, suppose you have an HTA (an HTML Application) that lists computer names; that HTA might allow you to select a computer and then perform some operation against it. After finishing the task, you might want to remove that computer from the list box; that way there won’t be any confusion about which computers you’ve used and which ones you haven’t. (This isn’t necessarily the best way to handle such a task, but it’ll do as a simple example.)

So how do you remove an option from a list box? Well, here’s an HTA that features a list box with 8 items. When you click the Clear List Box button, option 7 will be removed from the list box:

<title>Clear a List Box</title>

<SCRIPT Language=”VBScript”>

Sub ClearListbox For Each objOption in AvailableOptions.Options If objOption.Value = “7” Then objOption.RemoveNode Exit Sub End If Next End Sub


<body bgcolor=”buttonface”>

<select size=”8″ name=”AvailableOptions” style=”width:400″ > <option value=”1″>Option 1</option> <option value=”2″>Option 2</option> <option value=”3″>Option 3</option> <option value=”4″>Option 4</option> <option value=”5″>Option 5</option> <option value=”6″>Option 6</option> <option value=”7″>Option 7</option> <option value=”8″>Option 8</option> </select>

<p> <input id=runbutton class=”button” type=”button” value=”Clear List Box” name=”run_button” onClick=”ClearListbox”><p>

</body> </html>

We won’t spend much time talking about the code for creating an HTA; if you aren’t familiar with HTAs and what they can do, you might take a look at Part 1 in our HTA tutorial series. For today’s column, we’re going to concentrate on the subroutine named ClearListbox, the code that actually removes option 7 from the list box:

Sub ClearListbox
    For Each objOption in AvailableOptions.Options
        If objOption.Value = “7” Then
            Exit Sub
        End If
End Sub

Admittedly, we’re using a kind of brute-force method in order to grab – and remove – option 7, but that should be fine; unless you have tens of thousands of options in your list box your code will run plenty fast. When we say we’re using a brute-force method we simply mean this: we’re grabbing a list of all the items in the list box and then examining each one to see if the Value is equal to 7. This line of code retrieves a collection of all the items in the list box named AvailableOptions:

For Each objOption in AvailableOptions.Options

And this line of code checks each item to see if the Value is equal to 7:

If objOption.Value = “7” Then

And what if the value is equal to 7? In that case, we call the RemoveNode method to remove that option from the list box. And then, having done what we set out to do, we use this line of code to exit the subroutine:

Exit Sub

After all, having already found and removed the option in question, there’s no sense in looping through the remainder of the collection.

Keep in mind that this only works for removing items from a list box. We’re experimenting with a modified version that will loop through your life and remove items (and mistakes) from there, but that might take awhile. We’ll keep you posted.


Discussion is closed.

Feedback usabilla icon