|
-
Apr 13th, 2007, 08:42 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Listbox comparison
I am trying to add data inputted from a textbox to a listbox. My one stipulation is that if there is matching data in the listbox, then the data from the textbox will not be added to the listbox.
For example,
Data in listbox:
Balloon
Car
Cat
Dog
If a user tries to add “cat” from the textbox to the listbox, cat will not be added because it already exists. If a user tried to add “Horse” to the listbox, horse will be added because horse does not exist in the listbox.
Can someone please show me how this can be done? Thanks!
-
Apr 13th, 2007, 08:45 AM
#2
Lively Member
Re: Listbox comparison
I'll try to write some code, but it will be longwinded to make it case-sensitive, I know it should be straightforward to do it non-casesentsitive, meaning if they imput 'Dog', it won't go in, but if they imput 'dog', it will, since 'dog' isn't there, only 'Dog' is.
EDIT: You can try experimenting with an If Then statement. And if you have any code at the moment, post it.
I'm using VB 2005, what version are you using?
Last edited by SilencerXXX; Apr 13th, 2007 at 09:01 AM.
There is a thin line between hobby, and obsession.
If you found my post helpful, please rate it.
My Laptop System Stats: nVidia 8800 graphics card, Two Intel Core Duo processors, 3.5 GB RAM, Windows Vista Ultimate, 15.4 Widescreen Brightview display, Built-in Webcam and Microphone.
My Alienware System Stats: nVidia 8800 graphics card, Intel Core Duo Extreme Edition processors, 4 gb RAM, Windows Vista Ultimate, 21 Inch Plasma Display, 42 Inch LCD HD display.
-
Apr 13th, 2007, 09:03 AM
#3
Registered User
Re: Listbox comparison
vb Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ext As Boolean = False
For i As Integer = 0 To ListBox2.Items.Count - 1
If TextBox2.Text.Trim = ListBox2.Items.Item(i).ToString Then
MessageBox.Show("already exists")
ext = True
Exit Sub
End If
Next
If ext = False Then
ListBox2.Items.Add(TextBox2.Text)
End If
End Sub
Rate me if this code works fine.
-
Apr 13th, 2007, 09:20 AM
#4
Re: Listbox comparison
I can guarantee you that there is a hi-tech solution to this, but here is the low-tech solution:
(just a small addition to RaviIntegras code, to make remove the case-sensitiveness)
VB Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'There is no need for the boolean, because if an existing item is found in the listbox, it exits the sub, so the only time when it will reach the last line of the subroutine is when the Textbox2.Text string is not in the listbox.
For i As Integer = 0 To ListBox2.Items.Count - 1
If TextBox2.Text.ToLower = ListBox2.Items.Item(i).ToString.ToLower Then
MessageBox.Show("already exists")
Exit Sub
End If
Next
ListBox2.Items.Add(TextBox2.Text)
End Sub
-
Apr 13th, 2007, 09:21 AM
#5
Lively Member
Re: Listbox comparison
 Originally Posted by RaviIntegra
vb Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ext As Boolean = False
For i As Integer = 0 To ListBox2.Items.Count - 1
If TextBox2.Text.Trim = ListBox2.Items.Item(i).ToString Then
MessageBox.Show("already exists")
ext = True
Exit Sub
End If
Next
If ext = False Then
ListBox2.Items.Add(TextBox2.Text)
End If
End Sub
Rate me if this code works fine.
It's what I have, it should work fine in VB 2005, in earlier versions of VB, you would need to change the names of certain controls in the code itself. Just make sure you use a textbox and a listbox and a button in the form. You need to make sure the names of the text box, listbox and button are "Textbox2" and "Listbox2" and "Button2", or just use this code, I changed those already, so its easier, and I also added a title, and an error icon in the messagebox that pops up when the text is the same, hope you don't mind Ravi.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ext As Boolean = False
For i As Integer = 0 To ListBox1.Items.Count - 1
If TextBox1.Text.Trim = ListBox1.Items.Item(i).ToString Then
MessageBox.Show("Text already exists", "Text Already Exists", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
ext = True
Exit Sub
End If
Next
If ext = False Then
ListBox1.Items.Add(TextBox1.Text)
End If
End Sub
Atheist's code is less bulky, and I suggest you use his/hers (sorry, don't know your gender atheist). Just make sure you do what I said previously, have a button, listbox and textbox in the form, and name the button 'Button2', the textbox 'Textbox2' and the listbox 'Listbox2', or change the names in the code to your names for them
Last edited by SilencerXXX; Apr 13th, 2007 at 09:29 AM.
There is a thin line between hobby, and obsession.
If you found my post helpful, please rate it.
My Laptop System Stats: nVidia 8800 graphics card, Two Intel Core Duo processors, 3.5 GB RAM, Windows Vista Ultimate, 15.4 Widescreen Brightview display, Built-in Webcam and Microphone.
My Alienware System Stats: nVidia 8800 graphics card, Intel Core Duo Extreme Edition processors, 4 gb RAM, Windows Vista Ultimate, 21 Inch Plasma Display, 42 Inch LCD HD display.
-
Apr 14th, 2007, 09:40 PM
#6
Thread Starter
Addicted Member
Re: Listbox comparison
Thanks guys,
That is exactly what I was looking for.
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
|