Results 1 to 9 of 9

Thread: trying to create a search button for my listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    trying to create a search button for my listbox

    hello to all i am practicing creating a data file using streamwriter/reader. I am a beginner and what i am trying to do is create a data file and create a search button that will be able to search the data file and show in the listbox the item or items searched for.

    this is my code. i tried also using FindExactString method but i just cant seem to be able to do it. i dont get how to use or write it correctly. any help would be appreciated thank you in advance.

    Code:
    Imports System.IO
    Public Class Form1
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim pInventory As IO.StreamWriter = IO.File.AppendText("product.txt")
            If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
                MsgBox("Please fill in required fields", MsgBoxStyle.Exclamation, "Error")
            Else
                pInventory.WriteLine(TextBox1.Text & ", $" & TextBox2.Text & ", " & TextBox3.Text)
                TextBox1.Clear()
                TextBox2.Clear()
                TextBox3.Clear()
                Dim num As Integer
                For num = 0 To ListBox1.Items.Count - 1
                    pInventory.WriteLine(ListBox1.Items(num))
                Next
                pInventory.Close()
            End If
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
          Dim name As String
            Dim num As Integer
            Dim max As Integer
    name = InputBox("Enter a search word", "Search")
            num = 0
            max = ListBox1.Items.Count - 1
            'ListBox1.Items.Add(ListBox1.Items.Item(0))
            Do While (name <> (ListBox1.Items.Item(num))) And (num < max) **i keep getting an error in this line**
                num = num + 1
    
                ListBox1.Items.Add(num)
            Loop
            If (name = ListBox1.Items.Item(num)) Then
    MsgBox(name & " Was found", , "Search")
            Else
    MsgBox(name & " Was not found", , "Search")
            End If
    end sub
    end class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: trying to create a search button for my listbox

    Firstly, if n error occurs, please tell us what the error message is, not just that an error occurs, so that we know what we're looking for.

    As for the question, what is it that you're actually trying to achieve? Are you saying that you want to read the lines of a file and populate a ListBox with the line numbers of those that match some text entered by the user? Rather than just a general description, please tell us EXACTLY what it is that you want to happen rather than our having to work it out from code that doesn't actually do it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: trying to create a search button for my listbox

    vb.net Code:
    1. Do While (name <> (ListBox1.Items.Item(num))) And (num < max) **i keep getting an error in this line**
    2.             num = num + 1
    3.  
    4.             ListBox1.Items.Add(num) ' what?
    5.         Loop

    So if you find a matching item in the Listbox add it's number to the Listbox items making what presumably is a long list (otherwise why have a search facility at all) even longer? How does that make any sense?

    Do you have Option Strict On because that would certainly raise an error on that line because of all the implicit conversions required. I would probably use a For Each loop here and simply select any matches within the ListBox but then I'm really not sure what you're actually doing this for in the first place. What happens when you've found the item? The fact that it's in a ListBox means that there aren't really a lot of options going forward from that point.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    Re: trying to create a search button for my listbox

    OK, sorry about that. well firstly the error that i get is "exception was unhandled. it says its an invalid argument."
    I want the search button that i am creating to show the keyword searched for in the listbox. FOR EXAMPLE if i search for STREAMREADER then all the words with stream and reader will appear in the listbox like......STREAMwriter, STREAM, READERfile, etc. i know i have an example there that adds items to the listbox but its the only thing i have to work with. its from a previous assignment that i had to do and im trying to change things up but i cant figure it out, i tried using FindString but obviously im doing something wrong i figured i would work from my homework example again. see i am creating four buttons in total:
    1. an entry data button that saves words in a txt file.
    2. a view data button that shows all of the data entered into the txt file.
    3. a delete data button that deletes a selected word from the listbox.
    and
    4. a search data button that searches the txt file for words that may or may not exist in the text file...........this is the only one im having problems with. i was having problems with the view data button but i kept working and looking up information and finally figured it out but that damn search button i cant seem to get it...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: trying to create a search button for my listbox

    Quote Originally Posted by AA123 View Post
    FOR EXAMPLE if i search for STREAMREADER then all the words with stream and reader will appear in the listbox like......STREAMwriter, STREAM, READERfile, etc.
    Does that really make sense? That was rhetorical question; it doesn't make sense. How exactly is the program supposed to know that it's OK to break that search term up as "stream" and "reader" rather than "str" and "eamreader" or any other combination? Do you actually mean that you would enter the text "stream reader" and they would be treated as separate keywords because of the space? Please tell us what you actually mean so that we don't have to guess or, if you really did mean what you posted earlier, rethink things because that isn't logical.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    Re: trying to create a search button for my listbox

    Quote Originally Posted by jmcilhinney View Post
    How exactly is the program supposed to know that it's OK to break that search term up as "stream" and "reader" rather than "str" and "eamreader" or any other combination? Do you actually mean that you would enter the text "stream reader" and they would be treated as separate keywords because of the space?
    what i meant was: if somebody searches for streamreader all of the words that have streamreader in them will show up in the listbox. for example: if i search for streamreader and there is a word like helloiamSTREAMREADERhowareyou then this entire "word" ,helloiamSTREAMREADERhowareyou, will show up in the listbox. i am just trying to create a search method for my program. you search for a keyword and if it is in the text file then it will show up in the listbox. sorry i guess im so frustrated with this that i didnt realize how i worded my msg.

  7. #7
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: trying to create a search button for my listbox

    Quote Originally Posted by AA123 View Post
    what i meant was: if somebody searches for streamreader all of the words that have streamreader in them will show up in the listbox. for example: if i search for streamreader and there is a word like helloiamSTREAMREADERhowareyou then this entire "word" ,helloiamSTREAMREADERhowareyou, will show up in the listbox. i am just trying to create a search method for my program. you search for a keyword and if it is in the text file then it will show up in the listbox. sorry i guess im so frustrated with this that i didnt realize how i worded my msg.

    Whats your idea of the word meaning exact?

    Finds the first item in the ListBox that exactly matches the specified string.

    helloiamSTREAMREADERhowareyou does not EXACTLY match streamreader does it.
    My Github - 1d3nt

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    Re: trying to create a search button for my listbox

    Quote Originally Posted by ident View Post
    Whats your idea of the word meaning exact?

    Finds the first item in the ListBox that exactly matches the specified string.

    helloiamSTREAMREADERhowareyou does not EXACTLY match streamreader does it.
    what the hell are you talking about i didnt mention anything about exact.

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: trying to create a search button for my listbox

    Are you sure "i tried also using FindExactString method but i just cant seem to be able to do it."
    My Github - 1d3nt

Tags for this Thread

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