+ Reply to Thread
Results 1 to 4 of 4

How to distinguish between the first selected item in a combobox and no selected items

  1. #1
    Registered User
    Join Date
    03-01-2013
    Location
    Millbrook, Canada
    MS-Off Ver
    Excel 2010
    Posts
    6

    How to distinguish between the first selected item in a combobox and no selected items

    Hi all,

    I have a combobox on a userform where I would like the user to have the ability to add or delete items. To delete an item, I would like for the user to be able to select an item in the list and then click a [-] delete button. The problem that I'm experiencing is that .listindex = 0 when the first item is selected, but also when nothing is selected. How can I distinguish between the two situations to prevent the first item from being deleted when, in fact, nothing has been selected?

    The If ... Exit Sub line, in the code below, prevents the first item from being deleted when nothing is selected. But, also prevents the first item from being deleted altogether.

    PHP Code: 
    Private Sub RemoveTagButton_Click()
           
        If 
    Me.ListBoxTags.ListIndex <= 0 Then Exit Sub
           
        Me
    .ListBoxTags.RemoveItem (Me.ListBoxTags.ListIndex)
       
    End Sub 
    Thanks!

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: How to distinguish between the first selected item in a combobox and no selected items

    The ListIndex property is -1 when nothing is selected.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    03-01-2013
    Location
    Millbrook, Canada
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: How to distinguish between the first selected item in a combobox and no selected items

    Quote Originally Posted by shg View Post
    The ListIndex property is -1 when nothing is selected.
    Thanks shg. You're right. The problem was that after deleting an item from the list, Excel doesn't automatically return the listindex to -1, even though nothing is visibly selected.

    The solution in my case was to add an extra line of code to force the listindex back to -1 after each deletion.

    PHP Code: 
    Private Sub RemoveTagButton_Click()
           
        If 
    Me.ListBoxTags.ListIndex 0 Then Exit Sub
           
        Me
    .ListBoxTags.RemoveItem (Me.ListBoxTags.ListIndex)
        
    Me.ListBoxTags.ListIndex = -1

    End Sub 

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: How to distinguish between the first selected item in a combobox and no selected items

    Good job sorting it out. That would have surprised me as well.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1