-
Nov 29th, 2013, 12:10 AM
#1
Thread Starter
PowerPoster
Is there a way to search a list for an item, then remove it?
Hi there everyone. I am working on a math game where students can make their own custom game patterns. I am working on a way for students to be able to delete custom patterns down the road.
So far I have this..
VB Code:
Dim iczg As Integer
Dim srch_stringzg As String
srch_stringzg = LoadGameNameTXT.text 'or whatever you want to search for
For iczg = 0 To CustomGameList.ListCount - 1
If CustomGameList.List(iczg) = srch_stringzg Then
CustomGameList.RemoveItem srch_stringzg
End If
Next iczg
Basically the name of the pattern is in the LoadGameNameTXT.text. I am trying to get the code to search for it in the CustomGameList listbox, and if found remove it from the listbox, but I am getting a type mismatch error on this line..
CustomGameList.RemoveItem srch_stringzg
Can anyone tell me what I did wrong?
Thanks!!
-
Nov 29th, 2013, 01:19 AM
#2
Hyperactive Member
Re: Is there a way to search a list for an item, then remove it?
remove the item by index and not the string it looks like
change yours to
Code:
CustomGameList.RemoveItem iczg
-
Nov 29th, 2013, 03:33 AM
#3
Re: Is there a way to search a list for an item, then remove it?
Also process the List backwards, otherwise the indexing goes wrong!
-
Nov 29th, 2013, 03:45 AM
#4
Lively Member
Re: Is there a way to search a list for an item, then remove it?
Originally Posted by Arnoutdv
Also process the List backwards, otherwise the indexing goes wrong!
Honest question. No harm intended.
This is another area I don't have much experience in.
Does the conflict arise because a member of the list has been deleted, but the list hasn't been re-indexed and the program thinks that member is still there?
-
Nov 29th, 2013, 03:58 AM
#5
Re: Is there a way to search a list for an item, then remove it?
No problem Honduras!
The problem arises when you have 2 items in a row which need to be removed.
If you need to remove item 5 + 6, then after you remove item 5, item 6 will become the new item 5.
But the For Next loop sets your Counter to 6, so you skip the new item 5
Code:
Private Sub Command2_Click()
Dim cCol As Collection
Dim i As Long
Set cCol = New Collection
cCol.Add "AAA"
cCol.Add "BBB"
cCol.Add "BBB"
cCol.Add "CCC"
Debug.Print "---"
Debug.Print "Forwards"
Debug.Print "---"
' We want to remove all items with "BBB"
For i = 1 To cCol.Count - 1
If cCol(i) = "BBB" Then cCol.Remove i
Next i
For i = 1 To cCol.Count
Debug.Print cCol(i)
Next i
Debug.Print "---"
Debug.Print "Backwards"
Debug.Print "---"
Set cCol = New Collection
cCol.Add "AAA"
cCol.Add "BBB"
cCol.Add "BBB"
cCol.Add "CCC"
' We want to remove all items with "BBB"
For i = cCol.Count - 1 To 1 Step -1
If cCol(i) = "BBB" Then cCol.Remove i
Next i
For i = 1 To cCol.Count
Debug.Print cCol(i)
Next i
End Sub
-
Nov 29th, 2013, 05:07 AM
#6
Lively Member
Re: Is there a way to search a list for an item, then remove it?
So you have to take an action after ever deletion. Re-index or re-create the array? That's good to know and may explain some problems I have had in the past.
It's good to keep in mind that a program does things, and usually well. But there are some it doesn't do. Knowing the difference is what makes us all millionaires.
-
Nov 29th, 2013, 05:10 AM
#7
Re: Is there a way to search a list for an item, then remove it?
The action is, loop backwards.
Just test my example, it does both and shows the results.
-
Nov 29th, 2013, 08:11 AM
#8
Re: Is there a way to search a list for an item, then remove it?
Yep backwards is the ticket, this prevents the unchecked entries from changing their index numbers
So if you loop forwards and have 10 items in the list when you remove item 2 then item 3 becomes item 2 and you move on to item 3 which used to be item 4 so 3 is skipped over and not checked and then of course you will get an index out of bounds when you reach item 10 as it no longer exists.
Looping backwards the indexes change only on those items that have already been tested so you do not run into these issues
btw wasn't this same question asked by the Op and answered just a few days ago?
-
Nov 29th, 2013, 07:27 PM
#9
Thread Starter
PowerPoster
-
Nov 29th, 2013, 08:49 PM
#10
Re: Is there a way to search a list for an item, then remove it?
If List1.List(n) = "" Then List1.RemoveItem n
or if you are really checking for blank spaces (not null as above)
Dim Lgth As Integer
Lgth = Len(List1(n))
If List1.List(n) = Space(Lgth) Then.......
Last edited by jmsrickland; Nov 29th, 2013 at 08:57 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 29th, 2013, 09:17 PM
#11
Thread Starter
PowerPoster
Re: Is there a way to search a list for an item, then remove it?
Awesome thanks! Would n be dimmed as a string in this case?
-
Nov 29th, 2013, 10:22 PM
#12
Re: Is there a way to search a list for an item, then remove it?
No, n is an Integer which I used as the index I think you are using iczg
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Nov 29th, 2013, 11:11 PM
#13
Re: Is there a way to search a list for an item, then remove it?
One go
Code:
If Trim$(List1.List(n)) = "" Then List1.RemoveItem n
-
Nov 30th, 2013, 01:02 AM
#14
Re: Is there a way to search a list for an item, then remove it?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Dec 5th, 2013, 11:45 AM
#15
Thread Starter
PowerPoster
Re: Is there a way to search a list for an item, then remove it?
Thank you for the help.
Just as a follow up question. Lets say I have a textbox instead of listbox
Would
If Trim$(List1.List(n)) = "" Then List1.RemoveItem n
become
Dim
If Trim$(Text1.text(n)) = "" Then
Text1.text = Text1.text - n
end if
???
-
Dec 5th, 2013, 01:28 PM
#16
Re: Is there a way to search a list for an item, then remove it?
The Texbox.Text holds a single string. Not a string array.
-
Dec 5th, 2013, 07:39 PM
#17
Thread Starter
PowerPoster
Re: Is there a way to search a list for an item, then remove it?
Ok, to make things more complicated lets say the stuff I needed to remove was always in brackets (), and they are always infront/on the left side of the text I want to keep, is there a way to get rid of the text in the () including the brackets from the textbox?
-
Dec 5th, 2013, 08:03 PM
#18
Re: Is there a way to search a list for an item, then remove it?
Code:
s = "(123)45678"
s = Mid$(s, InStr(1, s, ")") + 1)
MsgBox s
-
Dec 5th, 2013, 09:52 PM
#19
Thread Starter
PowerPoster
Re: Is there a way to search a list for an item, then remove it?
Thanks, would "s" in this case be a string or the textbox with the original text?
-
Dec 5th, 2013, 09:59 PM
#20
Re: Is there a way to search a list for an item, then remove it?
Originally Posted by Justin M
Thanks, would "s" in this case be a string or the textbox with the original text?
Yes of course
-
Dec 5th, 2013, 11:43 PM
#21
Thread Starter
PowerPoster
Re: Is there a way to search a list for an item, then remove it?
Oh this is awesome!
So now my code looks like this.
Dim s As String
s = LoadGameNameTXT.text
s = Mid$(s, InStr(1, s, ")") + 1)
LoadGameNameTXT.text = s
Would there be a way for it to check for cases where there could be two sets of brackets? like ()()
-
Dec 6th, 2013, 11:01 AM
#22
Thread Starter
PowerPoster
Re: Is there a way to search a list for an item, then remove it?
For example, if I run the code twice would it get rid of the first set of brackets (), then if it runs again would it get rid the second set?
-
Dec 6th, 2013, 05:32 PM
#23
Re: Is there a way to search a list for an item, then remove it?
If you are sure the part of the string you want to keep doesn't contain ")" then you can use InStrRev to get rid of all brackets in one step.
Code:
LoadGameNameTXT.Text = Mid$(LoadGameNameTXT.Text, InStrRev(LoadGameNameTXT.Text, ")") + 1)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|