Results 1 to 8 of 8

Thread: list box removal

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    5

    Resolved list box removal

    hello, im new and i need help with a work project, its a very simple problem i have:

    i have created an example of the problem I have:

    when either the red or the blue check box is selected, the phrase corresponding to the colour in the list box needs to be removed, i.e all the words 'red' in the list box need to be removed when the red check box is selected.

    Thanks!

    Kellie

    Attached Images Attached Images  
    Last edited by kellievb; Nov 25th, 2004 at 05:20 PM.

  2. #2
    Member
    Join Date
    Oct 2004
    Posts
    43
    VB Code:
    1. Dim i As Integer
    2. For i = lstRedBlue.ListCount To 0 Step -1   'This means this will check each item in the box. The -1 is due to that the listbox adds 1 extra row.
    3. If chkRed.Value = 1 And lstRedBlue.List(i) = "Red" Then
    4. lstRedBlue.RemoveItem (i) 'If Red is checked then remove that row.
    5. End If
    6. If chkBlue.Value = 1 And lstRedBlue.List(i) = "Blue" Then
    7. lstRedBlue.RemoveItem (i)
    8. End If
    9. Next i

    [B]Updated
    Last edited by Forge; Nov 21st, 2004 at 12:32 PM.

  3. #3
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016
    when removing multiple items from a listbox you should loop the listbox in reverse.
    VB Code:
    1. Dim i As Integer
    2.  
    3. For i = List1.ListCount - 1 To 0 Step -1
    4.  If List1.List(i) = Check1.Caption Then
    5.   If Check1.Value = vbChecked Then
    6.    List1.RemoveItem i
    7.   End If
    8.  ElseIf List1.List(i) = Check2.Caption Then
    9.   If Check2.Value = vbChecked Then
    10.    List1.RemoveItem i
    11.   End If
    12.  End If
    13. Next i

    casey.

  4. #4
    Member
    Join Date
    Oct 2004
    Posts
    43
    Both our codes work, but chose which you find easiest to understand.

  5. #5
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016
    Forge, that is not correct because of 2 reasons.

    1. what if both checkboxes are ticked ?
    2. if two items are the same and next to each other in a listbox then only one will get removed because the loop isnt going in reverse.

    casey.

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    5
    EDIT: just me being a dummie, thanks for the help guys and gals

    kellie
    Last edited by kellievb; Nov 21st, 2004 at 12:35 PM.

  7. #7
    Member
    Join Date
    Oct 2004
    Posts
    43
    Your welcome : )

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by kellievb
    EDIT: just me being a dummie, thanks for the help guys and gals

    kellie
    If your question is answered then please help everyone out by editing the first post and adding the green checkmark. Thanks.

Posting Permissions

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



Click Here to Expand Forum to Full Width