Re: Greying out (disabling) items in a dropdown (or list) box




There really just isn't any way to show you how to
subclass in "a few lines".

It sounds like you don't care about actually
*visibly* disabling the box. The code I was suggesting
needs the subclass to paint the text as gray, in order to
make it appear disabled. To just disable an item you
don't actually need the subclass. Just detect the ItemData
value that you saved for that Item, and if it's supposed to
be a disabled item then in the box's Click() event, don't
run your normal click code. Just use the sub to deselect
or select the next item down or up that's not disabled.
That will give the appearance that the item can't be selected.

The following "air code" in a form shows what I mean. Add a
listbox and a button, then add this code:

Private Sub Command1_Click()
List1.AddItem "one"
List1.ItemData(0) = 0
List1.AddItem "two"
List1.ItemData(1) = 1
List1.AddItem "three"
List1.ItemData(2) = 0

End Sub

Private Sub List1_Click()
Dim i2
i2 = List1.ListIndex
If i2 = -1 Then Exit Sub
If List1.ItemData(i2) = 1 Then
List1.ListIndex = -1
End If
End Sub

Run the program and you'll see that you can't select
item "two". But it's visually confusing since item
"two" isn't actually grayed out. For that you'll need
subclasssing, and for *that* you'll need more than
a few lines of code. Subclassing is a big topic if you
haven't done it before.

(Sorry about the top posting - just following the established pattern
here)

Thanks mayayana, very useful link. It looks like I'll have to use
subclassing to be able to prevent a user from clicking on a 'disabled'
list item (to be determined by a flag set in the itemData property of
the list/combo box.

I am not sure how to do this though - could you give a few lines that
will show how I can subclass a combo/list box control so that I disallow
clicks/selection of items in the lis, based on the item data.

Additionally, I have a tree view control - and I want to disable some of
its branches (nodes) based on information stored in the Tag property
of the node - it would be cool if I could apply the same principle
(background/foregraound colours as displayed in the code you provided a
link to.

Anyway back to the original questions:

1). Could you elaborate (preferably by meas of a few lines of code) how
I may subclass a combo/listbox and disable specific items based on
ItemData property value (flag)

2). Could you elaborate (preferably by meas of a few lines of code) how
I may do the same (i.e. subclass a TreeView control) and disable
specific nodes based on the Node.Tag property value (flag)


mayayana wrote:

There's a simple sample here:

www.jsware.net/jsware/vbcode.php3#lbox

You should be able to find a number of samples
around for both listbox (and probably combobox as
well). Coloring text and/or background of listbox items
is a common thing. The link above is a
subclassed UserControl. It provides the option
to specify text color of a listbox item and uses the
optional ItemData (long) property of an item to keep
track of the colors. You set color and ItemData
value when adding an item. Then when the item is
clicked you can respond to it based on the ItemData
value. That provides a way to color some items with
gray text and to know, when an item is clicked, whether
whether you should treat it as "disabled" in your
Click sub.


Is it possible to disable (i.e. so they cannot be selcted - and are
'greyed out') - items in a dropdown box or a listbox?

I assume this should be possible using sub classing etc ... ?





.