Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Listindex strange problem

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default Listindex strange problem

"Amit Shanker" wrote in message
...
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)


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.appspro.com/

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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Listindex strange problem

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 !


"Rob Bovey" wrote in message
...
"Amit Shanker" wrote in message
...
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)


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.appspro.com/

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




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default Listindex strange problem

"Amit Shanker" wrote in message
...
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)?


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.appspro.com/

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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Listindex strange problem

Works perfect, thanks Rob.


"Rob Bovey" wrote in message
...
"Amit Shanker" wrote in message
...
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)?


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.appspro.com/

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






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Userform problem (Listindex) jgmiddel Excel Discussion (Misc queries) 1 April 26th 06 01:34 PM
Strange Problem Perry Excel Discussion (Misc queries) 7 April 12th 06 09:52 PM
Strange Problem... Sujesh Excel Discussion (Misc queries) 6 December 30th 05 02:56 PM
listbox listindex problem jacob Excel Programming 2 August 25th 04 08:46 AM
Strange problem Stuart[_8_] Excel Programming 1 October 3rd 03 12:14 AM


All times are GMT +1. The time now is 12:59 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"