Hi-lighting items in a multi-select List box

G

Guest

Greetings Gurus -
I have a multi-select list box, lstActivityCode.
When this list box is loaded with values, I need one or more value to be
hi-lighted. The same as if the user selects them.

lstActivityCode.Selected(CodeNumber-1) = True

Doesn't work.

And I can't see any other way to do it.

Anyone know how?

Thanks in advance -
Dan
 
N

Norman Jones

Hi Dan,

Try something like:

'=============>>
Private Sub UserForm_Initialize()
With Me.ListBox1
.RowSource = "A1: A20 "
.MultiSelect = fmMultiSelectMulti
.Selected(1) = True
.Selected(3) = True
End With
End Sub
'<<=============
 
G

Guest

Thanks Norman -
That's what I tried, and while it makes that particular list item "active"
(that is, it fires the _Change() event), it doesn't show up as hilighted.
What I am looking for is a visual indication that this partcular item (or
more than one) is selected already.
I tried changing the list box so that it showed check-boxes, but that also
doesn't show any selection.

I guess I need something that acts the same as if the user clicked on the
list item(s), as that is what hilights it during run time.

Dan
 
N

Norman Jones

Hi Dan,

Using the suggested code - for me - the Userform is
displayed and the second and fourth ListBox items
are selected / highlighted - the Selected property
returns a zero-based array.
 
G

Guest

Thanks again Norman -
I see what's going on but I don't know why. If you can, please let me know
if you have any ideas for troubleshooting this?

I have a loop, which is doing various things unrelated to this listbox.
Inside this loop, I check some values in the spreadsheet to determine which
items in the listbox need to be hilighted, and do it. So I need to be in some
sort of a loop I think.

If I move my code outside of this loop, and just force it to "select" items
in the listbox, it works. But inside the loop it does not. In fact, if I
"select" items inside my loop, it fires the listbox_Change() event. So I know
it's selecting the item(s). But on the list, nothing is hilighted for some
reason.
Even if I just tell it to select one arbitrary item in the list, like:
lstActivityCode.select(3)=TRUE
it doesn't hilight it.
But if I move this code just outside the loop that's running, it does work.

I am trying to isolated what's going on, but if you have any troubleshooting
tips, I'd love to hear them.

Thanks,
Dan
 
N

Norman Jones

Hi Dan,

If the suggested code works in isolation, and fails for
you when incorporated into your "unrelated" loop code,
it is probable that the problem resides in your loop code.

As we have seen nothing of your problematic code,
it is difficullt to be more helpful.
 
G

Guest

I found it -
It turns out that the listbox was not enabled (.enabled = FALSE) before the
code that "selected" the list items.
After this process, I enabled the control. And the hi-lighting was not
there, although the list item(s) were in fact activated.

If I enable this control before making these selections, they show up fine.

Thanks again for your help - sometimes all it takes is some simple code like
yours that "works" to help with troubleshooting!

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top