problems with my search feature
problems with my search feature
problems with my search feature
problems with my search feature
problems with my search feature
problems with my search feature problems with my search feature problems with my search feature problems with my search feature problems with my search feature problems with my search feature problems with my search feature problems with my search feature
problems with my search feature problems with my search feature
problems with my search feature
Go Back  Xtreme Visual Basic Talk > > > problems with my search feature


Reply
 
Thread Tools Display Modes
  #1  
Old 01-13-2014, 03:55 PM
fyah fyah is offline
Newcomer
 
Join Date: Jan 2014
Posts: 2
Default problems with my search feature


hello to all i have created a practice program and i cant seem to get it to work correctly. i have created an entry data button that stores words into a text file. i have created a search button that searches the text file (in this case product.txt) and if the word that was searched for exists it will show it in the listbox. my 3rd button is a delete button that deletes whatever word you choose from the list box. and the last button is a view data button that shows all of the words that were stored in the text file. I am having problems with button 2 (my search 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:



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 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")
        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
Reply With Quote
  #2  
Old 01-13-2014, 05:24 PM
Gruff's Avatar
Gruffproblems with my search feature Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

I'd say you are over complicating file IO.

Read everything once into a List(of string) variable.

Adjust this variable as needed while your program is running.

Save the variable to disk once when you close your program.
(Over writing the original file.)
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
Reply

Tags
loop, practice, search, streamreader


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
problems with my search feature
problems with my search feature
problems with my search feature problems with my search feature
problems with my search feature
problems with my search feature
problems with my search feature problems with my search feature problems with my search feature problems with my search feature problems with my search feature problems with my search feature problems with my search feature
problems with my search feature
problems with my search feature
 
problems with my search feature
problems with my search feature
 
-->