|
-
Oct 8th, 2000, 08:55 PM
#1
Thread Starter
Addicted Member
I've got a textbox a command button a listbox.
When i click the command button I want the text in text1.text to be entered in the listbox. That is easy. My question is how would I make it check to see if that same text is already in the listbox? I do not want the same thing in the listbox twice. So if that text is already in the listbox then it will not add it. I hope i didnt confuse anyone. all help is appreciated.
-
Oct 8th, 2000, 09:01 PM
#2
Hyperactive Member
I could give you the answer but then what would you learn from it???
Something that always rings true is "Give a man a fish and he eats today... Teach him to fish and he eats for life".
You have a problem... and you need to tackle it by deciding what you have to do.
- Confirm Value doesn't exist
So if you were to explain that out further what do you get.
- For each item currently in the list, it does not match the one I am about to put into the list.
So next you look at the resources you have available to do this :
- A list box
- A count of the items
- A structure for each item
So the natural solution would be in pseudo-code like this :
Code:
For each item in the list box
If the current item matches what I am about to enter
Don't enter it
Get out of this loop
End If
Next item
From there you should be able to structure either a function and pass it the listbox and the item to add and expect a boolean return saying if you can add it or not or you can create the code and just put it directly in.
Paste the code into a reply here once your done.
-
Oct 8th, 2000, 09:10 PM
#3
Try this:
Code:
Private Sub Command1_Click()
List1.AddItem Text1.text
For i = 0 To List1.ListCount - 1
If i = List1.ListCount Then Exit Sub
For j = 0 To List1.ListCount - 1
If j <> List1.ListCount Then
If LCase$(List1.List(i)) = LCase$ _
(List1.List(j)) And i <> j Then
List1.RemoveItem (j)
j = 0
End If
End If
Next j
Next i
End Sub
-
Oct 8th, 2000, 09:25 PM
#4
Hyperactive Member
Matthew... I hate to say this but I don't like your code.
To add something and then remove it is wasteful. Instead you should simply check to see if the thing you are about to add already exists and do nothing the moment you find it does.
ONLY if it does not exist should you ever add it.
Plus... how will he ever learn how to solve problems when answers are dropped in his lap... you can't learn that way.
Code:
Public Sub AddToList(List1 as ListBox, Item as String)
Dim i as Integer
For i = 0 to List1.ListCount - 1
If LCase$(List1.List(i)) = LCase$(Item) Then
Exit Sub
End If
Next i
List1.AddItem Item
End Sub
Its shorter, more consise, answers the question directly by doing exactly what was required and is easy to read.
-
Oct 8th, 2000, 09:29 PM
#5
Thread Starter
Addicted Member
I was about to post what I had put together then I noticed that matt posted some code.
I had the exact same thing except for
Code:
If LCase$(List1.List(i)) = LCase$ _
(List1.List(j)) And i <> j Then
List1.RemoveItem (j)
I would have not been able to figure out that part anyway, so thanks matt.
When i try to run the project with that code I get a variable not defined error. I'm do not know what all the error messages mean yet, so i really dont know how to solve it.
-
Oct 8th, 2000, 09:34 PM
#6
Argh..I'm sorry Gen-X. It did take me 10 minutes to reply. And I guess I was to late. Code only removes duplicates.
Gen-X is absolutely correct. Check for it first, than add.
-
Oct 8th, 2000, 09:34 PM
#7
Thread Starter
Addicted Member
Oh, sorry gen. I didnt see your post.
I came out with almost the same code doing it on my own, but I would have never figured it out anyway, so thanks for you guys help. I would have never thought about putting
Code:
If LCase$(List1.List(i)) = LCase$(Item) Then
in there. I still dont understand what the LCase$ is. Do you mind explaining the purpose of it, or what it does?
-
Oct 8th, 2000, 09:42 PM
#8
Hyperactive Member
LCase is for "lowercase".
It means that it converts the entire string into lower case letters so that when you compare it with something else you don't have to worry about getting incorrect answers because the strings have a capital at the start etc.
Code:
LCase$("FIRST") = LCase$("First") = LCase$("first") = LCase$("firST")
"FIRST" <> "First" <> "first" <> "firST"
So you can see using LCase allows you to compare different strings without worrying if they also have the exact same case throughout.
-
Oct 8th, 2000, 09:47 PM
#9
Thread Starter
Addicted Member
Ah, thanks. I fully understand now.
-
Oct 8th, 2000, 09:59 PM
#10
Thread Starter
Addicted Member
One more question about your code Gen.
You have :
Code:
Public Sub AddToList(List1 As ListBox, Item As String)
Dim i As Integer
For i = 0 To List1.ListCount - 1
If LCase$(List1.List(i)) = LCase$(Item) Then
Exit Sub
End If
Next i
List1.AddItem Item
End Sub
I'm wanting to use that in the command button, so how would I use it?
When I try this:
Code:
Private Sub cmdAdd_click()
AddToList
End Sub
I get error "Argument not Optional"
-
Oct 8th, 2000, 10:42 PM
#11
Thread Starter
Addicted Member
Would that be the proper way to use that?
-
Oct 8th, 2000, 10:54 PM
#12
Lively Member
Like this
You need to pass the arguments.
Code:
Private Sub cmdAdd_click()
AddToList List1, Text1.Text ' pass the name of your
'listbox and textbox here
End Sub
-
Oct 8th, 2000, 11:29 PM
#13
Hyperactive Member
The example kokopeli gave you is correct.
It sounds as if you have having problems in general with the concepts of "parameters" and how to pass them etc.
Basically a Parameter is something that you need to "pass into" or "get back from" when calling a function or subroutine.
In the example for your problem you have 2 parameters :
List1 - Which ListBox you want to actually update
Item - The text you are wanting to add.
What this means is that in order to use the AddToList Subroutine you need to "pass" both of these parameters when you call it... Without them it doesn't know what it is doing
By default all parameters are "ByRef" which means it gives a reference to these and inside the subroutine it can change the values. You need to change it to "ByVal" if you want it to only pass the value and not allow you to change the source (In the case that one of the parameters happens to be a variable).
A function is a "SPECIAL" subroutine that also allows you to return a special paramater called the "Return Value" and it is done by setting the name of the Function to the answer knowing that when you "call" this function you can assign it to variables.
I would have a read of the MSDN on Subroutines and Procedures including how to define them and what everything means..
-
Oct 9th, 2000, 12:39 AM
#14
Thread Starter
Addicted Member
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
|