List Box

K

kirkm

Could someone please explain how to consistently set the first
item in the list box to selected (i.e. highlit) without needing to
click on it?

I've tried

List1.Selected(0) = True
and
list1.listindex = 0

Both *seemed* to work, but occasionaly fail.

Thanks - Kirk

PS it's not a MultiSelect listbox.
 
V

Vergel Adriano

Kirk,

Either one of the code you identified should work. Where do you have it in
your code? Perhaps there are other lines that follow it which inadvertently
changes the selection. Assuming you're trying to do this when the form
loads, try putting it this way and place it as the last line of code in the
UserForm_Initialize:

If List1.ListCount > 0 Then List1.ListIndex = 0

Other things to consider:
1. If you don't unload the form and just hide it, the next time you try to
show it, the Initialize event will not fire and whatever was selected before
you hid the form will be the one selected when it shows up again.
2. Step through your code to see if any lines are inadvertently changing the
listbox selection.
 
J

JLGWhiz

I am wondering if placing the reset command in the code for the click event
wouldn't be better than in the initialize event. It is the click event that
selects the last ListIndex item, so if after the selection it is reset to 0,
the first item should be in the window for the next use, whether or not the
initialize event runs. Or am I out in left field?
 
J

JLGWhiz

As an after thought, it would be necessary to have executed any use of the
value of the current ListIndex selection prior to the reset if the reset is
put into the ListBox click event.
 

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