search button problem

aa2

New member
Joined
Jan 4, 2014
Messages
4
Programming Experience
Beginner
Hello to all i have created a practice program and i cant seem to get it to work correctly. i have created 4 buttons:
Button 1. Entry data - creates a text file and stores any words you want onto the text file.
Button 2.Search data - asks you to search for any random word (you have to type it in the inputbox), searches the entire text file (product.txt in my code) for that word. if the word exists in the text file then it will show in the listbox. if the word does not exist then a message will pop up saying word does not exist.
Button 3. Delete data - once you click on the view data button and all of the words in the text are shown in the listbox you can highlight any word to delete it from
the listbox.
Button 4. View data - once you save however many words you want onto the text file using the entry data button you click on this button the listbox will show all of the words that were saved in the text file.

I am having problems with button 2 (my search data button). the program crashes everytime i search for a word and the error is "The process cannot access the file 'product.txt' because it is being used by another process." would somebody be able to tell me what is going wrong? thank you in advance, i posted my code below:



VB.NET:
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 StreamWriter = 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 word As String
        word = InputBox("Please enter search word: ", "Search")

        Dim name As String = File.ReadAllText("product.txt")'''''here is where the error is********************************HERE
        Dim index As Integer = name.IndexOf(word)
        If index >= 0 Then

            MsgBox(word & " was found.", MsgBoxStyle.Exclamation)
            ListBox1.Text = word

        Else
            MsgBox(word & " does not exist.", MsgBoxStyle.Critical)

        End If
    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
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim archive As StreamReader = New StreamReader("product.txt", True)
        ListBox1.Items.Clear()
        While archive.Peek() > -1
            ListBox1.Items.Add(archive.ReadLine)
        End While
        archive.Close()
    End Sub
End Class
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
First things first, don't use names like Button1 and Button2. They mean absolutely nothing to anyone reading the code, including yourself when you leave that project and come back to it months later. Always use names that indicate the purpose. If Button2 is for searching then name it searchButton. Etc.

As for the issue, there may be similar problems else where but I can see one cause of your issue in the Button1 Click event handler. In that event handler, the very first thing you do is open the file. Only when the file is open do you check whether there is something to write to the file and then you only close the file if you write something. If there's nothing to write then you have opened the file for nothing and you don't close it again; both bad.

The solution is to only open the file if you're going to use it and to make sure that you close it again afterwards. One way to do the second thing there is to always create your short-lived disposable objects, e.g. a StreamWriter, with a Using statement. The object will then be implicitly disposed at the End using line and objects like a StreamWriter are closed when disposed.
 

aa2

New member
Joined
Jan 4, 2014
Messages
4
Programming Experience
Beginner
As for the issue, there may be similar problems else where but I can see one cause of your issue in the Button1 Click event handler. In that event handler, the very first thing you do is open the file. Only when the file is open do you check whether there is something to write to the file and then you only close the file if you write something. If there's nothing to write then you have opened the file for nothing and you don't close it again; both bad. The solution is to only open the file if you're going to use it and to make sure that you close it again afterwards.

OK. Done now I'm not getting the search button issue any more. I am a beginner, so I'm sorry but, I'm kinda confused here with what you said :
One way to do the second thing there is to always create your short-lived disposable objects, e.g. a StreamWriter, with a Using statement. The object will then be implicitly disposed at the End using line and objects like a StreamWriter are closed when disposed.

I don't completely understand. Would you be able to elaborate or give me an example?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,579
Location
Sydney, Australia
Programming Experience
10+
OK. Done now I'm not getting the search button issue any more. I am a beginner, so I'm sorry but, I'm kinda confused here with what you said :


I don't completely understand. Would you be able to elaborate or give me an example?

When someone gives you keywords, the first thing you do is search the web for those keywords. Being a programming beginner doesn't mean that you don't have experience using a computer or the web.

vb.net using statement - Bing
 
Top Bottom