0

Last Name Finder

what I'm trying to do is create a list of all peoples surnames where the 1st name = @name although this isn't the exact purpose its easier to try and explain the actual content. i'm aware it may be easier to use a data source, but I want to try and avoid that where possible.

Form_Load
ListBox1.Items.Add("Dean Smith")
ListBox1.Items.Add("John Jones")
ListBox1.Items.Add("David Johnson")
ListBox1.Items.Add("Samantha Thompson")
ListBox1.Items.Add("Claire Frost")
ListBox1.Items.Add("John Brown")

and then some sort of string manipulation to do the following on button_click

 if textbox1.text contains "John" then
  listbox2.items.add(Jones)
  listbox2.items.add(Brown)
else messagebox.show("No matches found")
end if

Thanks for any input.

1 Answer 1

1

What I've understood is that you are willing to use your textbox as a filter to the listbox. And this can be achieved by running the query to your db on every TextChanged().

So to give you some guidelines, you can proceed like this;

 private Names As List(Of String)

 Private Sub Form2_Load1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Names = New List(Of String)

    With Names
        .Add("Dean Smith")
        .Add("John Jones")
        .Add("John Brown")
    End With

    For Each Name As String In Names
        ListBox1.Items.Add(Name)
    Next 

 End sub

 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged


        ListBox1.Items.Clear()
        For Each s As String In Names

            If s.Substring(0, TextBox1.Text.Length).ToLower = TextBox1.Text.ToLower Then
                ListBox1.Items.Add(s)
            End If
        Next 


  End Sub

I hope this solution might help you achieve what you wanted. You can also apply direct filter to the List by using a delegate function, which will avoid you from looping again on TextChange().

Sign up to request clarification or add additional context in comments.

1 Comment

that's perfect, although it adds the whole name I only want it to add the string after the space so if its Dean Smith just add Smith

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.