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:
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
	
		
			
		
		
	
				
			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