Results 1 to 4 of 4

Thread: Removing Item From Listbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Removing Item From Listbox

    Hello people,

    I have a listbox that gets populated when a command button on the same form is clicked. When, the listbox gets populated, a listbox in another form is also getting populated at the same time.

    The problem is that when I try to remove an item from a listbox, I have an error. I cannot remove the item from the listbox in another form.

    These are the two codes that I tried.

    VB Code:
    1. Dim Nm123 As String
    2.     Nm123 = lstOrder.Text
    3.     Dim ABC As Single
    4.     ABC = -1
    5.     Dim Trial As String
    6.     Dim DEF As String
    7.     DEF = ABC + 1
    8.    
    9.     For i = 1 To frmPizzaChoices.lstItems.ListCount
    10.    
    11.         Trial = frmPizzaChoices.lstItems.List(DEF)
    12.        
    13.         If Trial = lstOrder.Text Then
    14.        
    15.         frmPizzaChoices.lstItems.RemoveItem (DEF)
    16.        
    17.         End If
    18.    
    19.     Next i

    This is a simpler code
    VB Code:
    1. frmPizzaChoices.lstItems.RemoveItem (lstOrder.ListIndex)

    Can someone please suggest a different way to display this.

  2. #2
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Removing Item From Listbox

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim i As Integer
    4.  
    5.     With frmPizzaChoices
    6.  
    7.         For i = 0 To .lstItems.ListCount - 1
    8.             If .lstItems.List(i) = lstOrder.Text Then
    9.                 .lstItems.RemoveItem (i)
    10.             End If
    11.         Next i
    12.        
    13.     End With
    14.  
    15. End Sub

  3. #3
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Removing Item From Listbox

    Probably better to use ,

    VB Code:
    1. For i = .lstItems.ListCount - 1 To 0 Step -1

    instead of ,

    VB Code:
    1. For i = 0 To .lstItems.ListCount - 1

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Removing Item From Listbox

    With this code also, i have one problem, the item doesn't get removed for some reason.

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