Results 1 to 6 of 6

Thread: [RESOLVED] Listbox comparison

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Resolved [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!

  2. #2
    Lively Member
    Join Date
    Apr 2007
    Location
    NYC
    Posts
    86

    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.

  3. #3
    Registered User RaviIntegra's Avatar
    Join Date
    Mar 2007
    Location
    Pondicherry, India
    Posts
    125

    Re: Listbox comparison

    vb Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Dim ext As Boolean = False
    3.         For i As Integer = 0 To ListBox2.Items.Count - 1
    4.             If TextBox2.Text.Trim = ListBox2.Items.Item(i).ToString Then
    5.                 MessageBox.Show("already exists")
    6.                 ext = True
    7.                 Exit Sub
    8.             End If
    9.         Next
    10.         If ext = False Then
    11.             ListBox2.Items.Add(TextBox2.Text)
    12.         End If
    13.     End Sub
    Rate me if this code works fine.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         '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.
    3.         For i As Integer = 0 To ListBox2.Items.Count - 1
    4.             If TextBox2.Text.ToLower = ListBox2.Items.Item(i).ToString.ToLower Then
    5.                 MessageBox.Show("already exists")
    6.                 Exit Sub
    7.             End If
    8.         Next
    9.             ListBox2.Items.Add(TextBox2.Text)
    10.     End Sub
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Lively Member
    Join Date
    Apr 2007
    Location
    NYC
    Posts
    86

    Re: Listbox comparison

    Quote Originally Posted by RaviIntegra
    vb Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Dim ext As Boolean = False
    3.         For i As Integer = 0 To ListBox2.Items.Count - 1
    4.             If TextBox2.Text.Trim = ListBox2.Items.Item(i).ToString Then
    5.                 MessageBox.Show("already exists")
    6.                 ext = True
    7.                 Exit Sub
    8.             End If
    9.         Next
    10.         If ext = False Then
    11.             ListBox2.Items.Add(TextBox2.Text)
    12.         End If
    13.     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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    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
  •  



Click Here to Expand Forum to Full Width