Remove Unselected Items from listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • esperanto234
    New Member
    • Sep 2009
    • 14

    Remove Unselected Items from listbox

    Hi,
    I need a code to remove all the items that are unselected from a listbox.
    I really need this!!
    Thanks!!
  • vb5prgrmr
    Recognized Expert Contributor
    • Oct 2009
    • 305

    #2
    You will need to loop through the list box from listcount - 1 to 0 step -1 and check the selected property and if not selected use the remove method.



    Good Luck

    Comment

    • esperanto234
      New Member
      • Sep 2009
      • 14

      #3
      Thank you very much, I already did it.

      In case someone wants to know, the answer is the following:

      If NOM <> "" Then
      Dim ix As Integer
      Dim bFound As Boolean

      For ix = 0 To ctlCHN.ListCoun t - 1
      If ctlCHN.List(ix) = NOM.Text Then
      bFound = True
      Exit For
      End If
      Next

      If bFound = False Then
      ctlCHN.AddItem NOM.Text
      End If
      End If

      Comment

      • smartchap
        New Member
        • Dec 2007
        • 236

        #4
        Dear esparanto234 I think the question asked by u & code posted by u are not same. In ur code actually u are checking whether textbox entry already exists in the listbox or not, if not then it is added to the listbox. Deletion is no where done in ur code. To delete the unselected items, u need to loop through index=0 to index=Listcount-1 and check if a particular index item is selected or not. If it not selected then delete it (but adjust loop accordingly as now listbox has 1 item less than previous). So through loop u can delete unselected item(s).

        Comment

        Working...