0

Title says it all, I need help making my search input box find the title and display it when the user only types part of the movie title before searching. This is what I have now and it works great, but you must type in the complete title. Any help would be appreciated! Thanks

    Private Sub btnSearch_Click(sender As Object, e As EventArgs)Handles btnSearch.Click

    'Searches for movie in listbox
    Dim strDVDtitle As String

    strDVDtitle = InputBox("Enter Movie Title:")

    Dim X As Integer = 0

    Dim bolDVDFound As Boolean = False


    For X = 0 To count - 1
        If DVDS(X).DVDtitle = strDVDtitle Then
            txtDVDyear.Text = DVDS(X).DVDyear
            txtDVDtitle.Text = DVDS(X).DVDtitle
            txtDVDyear.Text = DVDS(X).DVDyear
            txtDVDruntime.Text = DVDS(X).DVDruntime
            txtDVDrating.Text = DVDS(X).DVDrating
            bolDVDFound = True
        End If
    Next

    If bolDVDFound = False Then
        MessageBox.Show("Movie not found")

    End If
End Sub 
wimh
  • 15,072
  • 6
  • 47
  • 98
  • Possible duplicate of [vb.net: finding a position of a string within a string?](http://stackoverflow.com/questions/1639381/vb-net-finding-a-position-of-a-string-within-a-string) – wimh Mar 07 '16 at 23:43
  • I tried indexof... didn't work. I just need to compare the entered text of the input box to DVDtitles in my Structure to find a title. – ZweiiHander Mar 07 '16 at 23:48
  • What did you try? Something like: `If DVDS(X).DVDtitle.IndexOf(strDVDtitle) > -1 Then`? – wimh Mar 07 '16 at 23:54
  • 1
    Hey Wimmel, The contains method worked great!!! Thanks guys – ZweiiHander Mar 07 '16 at 23:59
  • @ZweiiHander There is no reason that indexOf wouldn't have also worked, but you would need to remember to force all strings to lower or uppercase before checking, since strings are case-sensitive (the <> The). So the line should have been: `If DVDS(X).DVDtitle.ToLower().IndexOf(strDVDtitle.ToLower()) > -1 Then` – Scott Marcus Mar 08 '16 at 00:15
  • @Scott Okay, I see what you mean now. What do you think is more efficient? – ZweiiHander Mar 08 '16 at 00:19
  • @ZweiiHander .NET has many optimizations built-in to the compiler. The performance implications of indexOf vs. Contains would be negligible. Either is fine. – Scott Marcus Mar 08 '16 at 02:20

1 Answers1

1

You can use the contains string method, like this:

Dim actualTitle = "The Martian"

If actualTitle.ToLower().Contains(strDVDtitle.ToLower()) Then
  MsgBox("Match!")
End If
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • Awesome Thanks Scott :) – ZweiiHander Mar 07 '16 at 23:57
  • @ZweiiHander No problem. Don't forget to mark my post as the answer. Good luck! – Scott Marcus Mar 08 '16 at 00:12
  • Instead of calling `ToLower` twice I recommend using `actualTitle.IndexOf(strDVDtitle,StringComparison.OrdinalIgnoreCase)`. This is the more robust and convenient way to compare strings case-insensitive. See [this answer](http://stackoverflow.com/a/444818/2882256). – Alex B. Mar 08 '16 at 11:45