Problems with List box programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anukraju07
    New Member
    • Jan 2008
    • 12

    Problems with List box programming

    Hi,

    I would like to know how an item can be removed from a list box which contains more than one item. I'm getting an error if select 2nd or 3rd item for removal.
    Please help me
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    please try to use

    [CODE=vb]list1.removeite m list1.listindex[/CODE]

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Remember that the index goes from 0 to one less than the number of items. So if you are trying to remove the last one, you may be using too high a number. What error do you get? And what version of VB is this?

      Comment

      • anukraju07
        New Member
        • Jan 2008
        • 12

        #4
        Hi Mr.Killer42,

        I'm using VB 6.0 and is getting the fault message as" run time error 381 and invalid property array index.

        regards

        Comment

        • debasisdas
          Recognized Expert Expert
          • Dec 2006
          • 8119

          #5
          kindly post the code you are working on.

          Comment

          • anukraju07
            New Member
            • Jan 2008
            • 12

            #6
            Problems with List box programming

            Hi,

            I'm making a programme in vb6.0 and got stuck up at a place where I want to remove items from a list box by pressing a command button after selecting the item in list box. I'm able to remove if only one item is there otherwise getting an error as " run time error 381 and invalid property array index." the code I tried is

            [CODE=vb]Dim i
            For i = 0 To List8.ListCount - 1
            If List8.Selected( i) Then
            List8.Removeite m List8.Listindex
            End If
            Next i[/CODE]

            please help me
            Last edited by Killer42; Feb 29 '08, 12:44 AM. Reason: Added CODE=vb tag

            Comment

            • debasisdas
              Recognized Expert Expert
              • Dec 2006
              • 8119

              #7
              Are you trying to remove all the items from the list ?

              Comment

              • anukraju07
                New Member
                • Jan 2008
                • 12

                #8
                Originally posted by debasisdas
                Are you trying to remove all the items from the list ?
                No. One by one at random. The main problem if I remove item with index 0

                Comment

                • debasisdas
                  Recognized Expert Expert
                  • Dec 2006
                  • 8119

                  #9
                  try this

                  [code=vb]
                  if list1.listindex <> -1 then
                  list1.removeite m list1.listindex
                  end if
                  [/code]

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by anukraju07
                    [CODE=vb]Dim i
                    For i = 0 To List8.ListCount - 1
                    If List8.Selected( i) Then
                    List8.Removeite m List8.Listindex
                    End If
                    Next i[/CODE]
                    I think this code will work, if you reverse the For loop as shown below. If you think about it, you are counting from, say, 0 to 5. But if you remove an item along the way, all the rest will shift back one place. So not only will you skip over the subsequent one, but you will try to go one place too far.
                    [CODE=vb]Dim i
                    For i = List8.ListCount - 1 To 1 Step -1
                    If List8.Selected( i) Then
                    List8.Removeite m i
                    End If
                    Next i[/CODE]Note, I also changed the index of the item to be removed to i. I believe this will work better if you allow multiselect in the listbox.

                    Comment

                    • anukraju07
                      New Member
                      • Jan 2008
                      • 12

                      #11
                      Originally posted by Killer42
                      I think this code will work, if you reverse the For loop as shown below. If you think about it, you are counting from, say, 0 to 5. But if you remove an item along the way, all the rest will shift back one place. So not only will you skip over the subsequent one, but you will try to go one place too far.
                      [CODE=vb]Dim i
                      For i = List8.ListCount - 1 To 1 Step -1
                      If List8.Selected( i) Then
                      List8.Removeite m i
                      End If
                      Next i[/CODE]Note, I also changed the index of the item to be removed to i. I believe this will work better if you allow multiselect in the listbox.
                      Hi Killer42,

                      Thanx a lot. It is working perfectly with a small change. The for loop lower limit has to be changed to 0 instead of 1 as otherwise the first one is not getting removed.

                      Regards

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by anukraju07
                        It is working perfectly with a small change. The for loop lower limit has to be changed to 0 instead of 1 ...
                        Good catch! :)

                        Comment

                        Working...