Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

Rewriting Textfile with WriteAllLines method

$
0
0
I am working on assignment in which I have two listboxs. One listbox is populated with the contents of a textfile, and the other has a blank textfile associated with it. There are two buttons to move the information back and forth. I am having an issue with removing the information from the textfile once the information has been transferred over to the other listbox/textfile.

Imports System.IO
Public Class Assignment_7


Private Sub Assignment_7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sw As IO.StreamWriter = IO.File.CreateText("AutosInStock.txt")
sw.WriteLine("Ford Focus 2005")
sw.WriteLine("Honda Accord 1997")
sw.WriteLine("Mustang GT 2007")
sw.WriteLine("Mazda CX-5 2012")
sw.WriteLine("Mazda-3 2013")
sw.Close()

Dim swSold As IO.StreamWriter = IO.File.CreateText("AutosSold.txt")
swSold.Close()

Dim autosInStock() As String = IO.File.ReadAllLines("AutosInStock.txt")
lstStock.Items.Clear()
lstStock.Items.AddRange(autosInStock)
End Sub

Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
'Quit form
Me.Close()
End Sub

Private Sub btnSell_Click(sender As Object, e As EventArgs) Handles btnSell.Click

Dim autos As String = lstStock.SelectedItem
Dim AutoInStock As String = ""
Dim item As String = ""

If lstStock.SelectedItem Is Nothing Then
MessageBox.Show("Please select a automobile")
Exit Sub
Else

Dim swAutoSold As StreamWriter = File.AppendText("AutosSold.txt")
swAutoSold.WriteLine(autos)
swAutoSold.Close()

Dim swSold() As String = IO.File.ReadAllLines("AutosSold.txt")
lstSold.Items.Clear()
lstSold.Items.AddRange(swSold)

RemoveFromStock(AutoInStock, item)

End If

End Sub


Private Sub btnRepo_Click(sender As Object, e As EventArgs) Handles btnRepo.Click
Dim repo As String = lstSold.SelectedItem


If lstStock.SelectedItem Is Nothing Then
MessageBox.Show("Please select a automobile")
Exit Sub
End If

Dim swAutoRepo As StreamWriter = File.AppendText("AutosInStock.txt")
swAutoRepo.WriteLine(repo)
swAutoRepo.Close()
Dim swRepo() As String = IO.File.ReadAllLines("AutosInStock.txt")
lstStock.Items.Clear()
lstStock.Items.AddRange(swRepo)
End Sub

Sub RemoveFromStock(ByVal AutosInStock As String, ByVal item As String)
Dim query = From autoInStock In IO.File.ReadAllLines("AutosInStock.txt")
Where autoInStock <> item
Select autoInStock
IO.File.WriteAllLines("AutosInStock.txt", query.ToArray)
End Sub

End Class

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>