Listindex strange problem

Listindex strange problem

Post by Amit Shank » Fri, 11 Feb 2005 02:24:33


Hi,

FYI, Office 2003 on WinXP Pro, populating a userform listbox via AddItem
method.

In the listbox, even when I can plainly see that *no* item is selected, I
get a value of 0 (zero) which indicates that the first item *is* selected :
?fmchtlist.chtlistbox.listindex (this code in the immediate window, executed
when the userform is loaded, and the listbox is populated via code BUT no
item is yet selected)

Any ideas thankfully received !

Amit
 
 
 

Listindex strange problem

Post by Rob Bove » Fri, 11 Feb 2005 03:00:34


Hi Amit,

Is this a multi-select listbox? If so, this is the expected behavior. In
a multi select listbox the ListIndex property refers to the currently
highlighted item in the list (not necessarily the same as selected, but
rather the one surrounded by the dotted line). Unless you specifically set
the ListIndex to some other value, the first item in the list will be
highlighted after loading the listbox from VBA.

--
Rob Bovey, Excel MVP
Application Professionals
http://www.yqcomputer.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.yqcomputer.com/

 
 
 

Listindex strange problem

Post by Amit Shank » Fri, 11 Feb 2005 03:17:47

Hi Rob,

Yes, it is indeed a multiselect listbox, with a checkbox shown against each
item. If this is expected behaviour, then perhaps you could suggest a
solution to my following problem :

What I need to do is this : In this multiselect listbox, if no item is
checked, then when I click the OK button on the userform, it should simply
unload the userform and quit. How do I trap the fact that no check mark is
there against any item (so far of course I was trying to use ListIndex
= -1)?

Thanks !
 
 
 

Listindex strange problem

Post by Rob Bove » Fri, 11 Feb 2005 03:24:27


Hi Amit,

In a multi-select listbox you need to loop through each item in the list
and check the Selected property of each to determine if any items are
selected. Here's an example:

Private Sub CommandButton1_Click()
Dim bSelected As Boolean
Dim lIndex As Long
For lIndex = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lIndex) Then bSelected = True
Next lIndex
If Not bSelected Then Unload Me ''' Nothing selected.
End Sub

--
Rob Bovey, Excel MVP
Application Professionals
http://www.yqcomputer.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.yqcomputer.com/
 
 
 

Listindex strange problem

Post by Amit Shank » Fri, 11 Feb 2005 03:53:00

Works perfect, thanks Rob.