Question Listbox Search and Add

Sammy79

New member
Joined
May 3, 2008
Messages
3
Programming Experience
Beginner
Hi,

I'm very new to VB and I'm completing a small program uses a listbox. What has me totally stumped. I want to be able to:

  • enter a string into a textbox
  • when executed checks the string is not already added to the listbox
  • if the string already exists = error message
  • if it does not exist - add it to the list box.

Below is my code so far but I am getting exception errors:
VB.NET:
Private Sub AddTextBoxButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddTextBoxButton.Click

        Dim vSearchValue As String
        vSearchValue = ValueTextBox.Text
        SearchList(vSearchValue)
    End Sub
    Function SearchList(ByVal vSearchValue As String) As Boolean
        Dim vIndex As Integer
        Dim vFoundMatch As Boolean = False
        For vIndex = 0 To ValueListBox.Items.Count - 1
            If ValueListBox.Items(vIndex) = vSearchValue Then
                vFoundMatch = True
                Exit For
            End If
        Next
        If vFoundMatch = False Then
            ValueListBox.Items.Add(vFoundMatch)
        Else
            MessageBox.Show("Subject Code already in the list")
        End If
    End Function

Any help would be great. apologies if i have posted incorrectly!
Sam
 
VB.NET:
If myListBox.FindStringExact(myString) = ListBox.NoMatches Then
    myListBox.Items.Add(myString)
Else
    MessageBox.Show(myString & " is already in the list.", "Duplicate Item")
End If
 
Hi there,

Thanks for that, however I need to have a function in this code. It's for an assignment. I have the following code however at first i was getting a conversion error, so i added the Cstr now it adds the string to the listbox but does not check to see if it exists. My code is below, and I can't figure out what I am doing wrong:

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] AddTextBoxButton_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] AddTextBoxButton.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] vSearchValue [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]vSearchValue = [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2](ValueTextBox.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] SearchList(vSearchValue) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]MessageBox.Show([/SIZE][SIZE=2][COLOR=#800000]"ERROR: The subject is already in the list"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]ValueListBox.Items.Add(vSearchValue)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] SearchList([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] vSearchValue [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] vIndex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] vFoundMatch [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] vIndex = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] ValueListBox.Items.Count - 1[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] ValueListBox.Items(vIndex) = [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2](vSearchValue) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]vFoundMatch = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2]vFoundMatch = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] vFoundMatch[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]

If you could point me in the right direction that would be great.

Thanks,
Sam
 
VB.NET:
Private Sub AddTextBoxButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddTextBoxButton.Click
  Dim vSearchValue As String = ValueTextBox.Text
  If SearchList(vSearchValue) Then
    MessageBox.Show("ERROR: The subject is already in the list")
  Else
    ValueListBox.Items.Add(vSearchValue)
  End If
End Sub

Private Function SearchList(ByVal vSearchValue As String) As Boolean
  Dim FoundMatch As Boolean = False
  Dim Counter As Integer = 0I
  Do While FoundMatch = False AndAlso Counter <= ValueListBox.Items.Count - 1
    If ValueListBox.Items(Counter) = vSearchValue Then
      FoundMatch = True
    End If
  Loop
  Return FoundMatch
End Function
 
Hi,
I tried your suggestion above and it still doesn't work. It just crashes now.
It's really frustrating as I have been staring at this code for 3 days now and i can't get it working.

Is it the boolean value where i am going wrong?
or something small i am missing?

Any help is much appreciated.
 
Back
Top