search a text file

aa2

New member
Joined
Jan 4, 2014
Messages
4
Programming Experience
Beginner
I finally fixed my code and all that's left for me to do is to create a search button. what I'm trying to create is a search button but i just cant figure it out and i need help doing it. I am a beginner. I just want the program to ask the user what he would like to search for, when the search button is clicked on. when the user types in the word he wants to search for.....i want the program to look in the text file to see if that word exists and if the word exists i want that word to appear in my listbox. The problem with this is that i have nothing to work off of (everything I have written here is from previous homework assignments that I had to mess around with to get them to work.) This program has no purpose, this is just practice for me and if you could be able to help me i would be extremely grateful. If not thank you for taking the time to look at my post. Here is my code so far, if you see anything wrong with what i have written so far feel free to give me some advice to make it better. Thank you. (I have not created a search button):



VB.NET:
Imports System.IO
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
            Dim archive As StreamWriter = New StreamWriter("stuff.txt", True)
    
            ListBox1.Items.Add(TextBox1.Text)
            ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
            archive.WriteLine(TextBox1.Text)
            archive.Close()
            MsgBox("ok")
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            Dim archive As StreamReader = New StreamReader("stuff.txt", True)
            ListBox1.Items.Clear()
            While archive.Peek() > -1
                ListBox1.Items.Add(archive.ReadLine)
            End While
            archive.Close()
        End Sub
    
    
        Public Sub DeleteLine(ByRef fileaddress As String, ByRef listbox1_selecteditem As String)
            Dim thefilelines As New List(Of String)
            thefilelines.AddRange(File.ReadAllLines(fileaddress))
            thefilelines.Remove(listbox1_selecteditem)
            File.WriteAllLines(fileaddress, thefilelines.ToArray)
    
        End Sub
    
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            DeleteLine("product.txt", ListBox1.SelectedItem)
            ListBox1.Items.Remove(ListBox1.SelectedItem)
    
            MsgBox("Item Deleted")
    
        End Sub
    
    End Class
 
the text file is created by saving any word you want to it and then the search button will have an inputbox pop up and ask the user to type in any random word he would like to search for and if the word exists in the text file it will show in the listbox. if the word does not exist then a message box will appear saying the word does not exist in the text file. I wrote code for the search button and reposted with better details here is the link and thank for your help i appreciate it a lot.

link: http://www.vbdotnetforums.com/vs-net-general-discussion/59605-search-button-problem.html#post165462
 

Latest posts

Back
Top