Results 1 to 10 of 10

Thread: Still with the listbox.additem / removeitem prob...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2000
    Posts
    70

    Unhappy

    Hey guys.. i need someone to help here
    I add an item to the listbox, i add it with an index, store the index in my array, then increment the index like so:
    Code:
    lstUsers.AddItem UsrName, CurIdx
    UserNameArray(CurIdx) = UsrName
    CurIdx = CurIdx + 1
    Then, i remove an item, using its index, i find the item in my array, so i know its index, then i do like so:
    Code:
    lstUsers.RemoveItem CurIdx
    This all works fine, until i try to add an entry which has previously been in and removed... any ideas??
    Daniel Rose
    VB 5.0 Enterprise.
    irc:irc2.dynam.ac

    If TheCodeInTheSig() Is Not Lame() Then IDontKnowWhatIs()

  2. #2
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    Maybe de-increment the CurIndex prop after removing?
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2000
    Posts
    70
    it wont work, i dont nessacarily remove the end one, so if i decrement it, i may have a duplicate.
    Daniel Rose
    VB 5.0 Enterprise.
    irc:irc2.dynam.ac

    If TheCodeInTheSig() Is Not Lame() Then IDontKnowWhatIs()

  4. #4
    Junior Member
    Join Date
    Oct 2000
    Posts
    19
    Why not let the software take care of the maintenance itself? You should not have to keep track of the values in an array etc. Try this sample code. Granted it is pretty rough, but I just threw it together to show you the software will take care of "array" manipulation itself. Please note that List1.Text is the same as your UsrName, List1.ListIndex is the same as your CurIdx, and List1.ListCount is the number of items in your "array". Also note that to delete an item you will have to enter a value of 0 through (List1.ListCount - 1)

    Private Sub Command1_Click()
    Dim i As Integer
    Dim x
    For i = 0 To 4
    List1.AddItem "A" & i
    Next i
    End Sub

    Private Sub Command2_Click()
    List1.RemoveItem (Text1.Text)
    End Sub

    Private Sub List1_Click()
    MsgBox "Text " & List1.Text & " Index " & List1.ListIndex & " Count " & List1.ListCount
    End Sub

    Hope this helps!!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2000
    Posts
    70
    aha. would be nice, if we could remove them on text, but alas, remove item requires the index
    Daniel Rose
    VB 5.0 Enterprise.
    irc:irc2.dynam.ac

    If TheCodeInTheSig() Is Not Lame() Then IDontKnowWhatIs()

  6. #6
    Junior Member
    Join Date
    Oct 2000
    Posts
    19
    Like so????

    Private Sub Command1_Click()
    Dim i As Integer

    For i = 0 To 4
    List1.AddItem "A" & i
    Next i
    End Sub

    Private Sub List1_Click()
    List1.RemoveItem (List1.ListIndex)
    End Sub

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2000
    Posts
    70
    ListIndex? I fail to see how that dictates the one that i want to remove... It doesnt get removed on a click event, see :P its removed on an event from the server, so i need to know the index
    Daniel Rose
    VB 5.0 Enterprise.
    irc:irc2.dynam.ac

    If TheCodeInTheSig() Is Not Lame() Then IDontKnowWhatIs()

  8. #8
    Junior Member
    Join Date
    Oct 2000
    Posts
    19

    Red face

    Ok,
    You will have to do some tweaking, but it should work. The Command 1 button just fills the array and list box. Command 2 simulates the userid name being passed from the server; just type in the "userid", A0-A4, to be deleted. If this isn't what you are looking for, sorry
    ---------------------------

    Dim arr(10) As String
    Private Sub Command1_Click()
    Dim i As Integer

    For i = 0 To 4
    'Fill the list box and array
    List1.AddItem "A" & i
    arr(i) = "A" & i
    Next i
    End Sub

    Private Sub Command2_Click()
    Dim x As String
    Dim j As Integer, i As Integer
    'x is the userid passed from the server
    x = InputBox("User to be deleted")
    For i = 0 To List1.ListCount - 1
    If arr(i) = x Then
    j = i
    End If
    Next i
    'get rid of the user
    List1.RemoveItem (j)
    'Now move the rest of the users up one index in the array
    For i = j To List1.ListCount
    arr(i) = arr(i + 1)
    Next i
    End Sub

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2000
    Posts
    70

    Wink

    thats what i'm looking for all right
    the prob is that the removeitem method just wont let me do it it requires a stupidass index :/
    Daniel Rose
    VB 5.0 Enterprise.
    irc:irc2.dynam.ac

    If TheCodeInTheSig() Is Not Lame() Then IDontKnowWhatIs()

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2000
    Posts
    70
    ok guys, i figured it out, you need to use the next available index to add it. starting at 0. i have made a few functions to do so, yer welcome to use them if you ever need to
    Code:
    Private IndexAvail(15) as Integer
    Private Sub UnusedIdx(Index As Integer)
    Static curidx As Integer
        IndexAvail(curidx) = Index
        If curidx = 15 Then curidx = 0
    End Sub
    and
    Code:
    Private Function GetNextIdx()
    dim idx as integer
    idx = 0
    For i = 1 To 15
        If IndexAvail(i) > idx Then idx = IndexAvail(i)
    Next i
    GetNextIdx = idx
    End Function
    then, when adding an item, simply call getnextidx() to get the next available index, and when removing an item, call unusedidx(index as integer) with the index of the one you removed, and it will mark it as free
    Daniel Rose
    VB 5.0 Enterprise.
    irc:irc2.dynam.ac

    If TheCodeInTheSig() Is Not Lame() Then IDontKnowWhatIs()

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