-
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??
-
Maybe de-increment the CurIndex prop after removing?
-
it wont work, i dont nessacarily remove the end one, so if i decrement it, i may have a duplicate.
-
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!!
-
aha. would be nice, if we could remove them on text, but alas, remove item requires the index ;)
-
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
-
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 :(
-
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
-
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 :/
-
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 :)