Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Remove a row from a csv file using a SelectedIndexChanged listbox.

$
0
0

I have a local CSV file that is appended by the user entering a 'Destination name into a 'listbox1, and 'Round trip miles' into listbox2 and then the file is saved using:

 System.IO.File.AppendAllText(Form1.GlobalVariables.DestinationFile, vbCrLf & dname & "," & dmiles)

My question is how to remove a complete CSV row using only the 'Destination entry' the user selects from a dialog form, listbox1 of 'destination names. 

Example CSV List:

 

Here is the current code:

 Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged


        Dim fileIn As String = Form1.GlobalVariables.DestinationFile
        Dim fileRows(), fileFields() As String
        If File.Exists(fileIn) Then
            Dim fileStream As StreamReader = File.OpenText(fileIn)
            fileRows = fileStream.ReadToEnd().Split(Environment.NewLine)
            For i As Integer = 0 To fileRows.Length - 1
                fileFields = fileRows(i).Split(",")

                Form1.ListBox1.Items.Remove(fileFields(0))
                Form1.ListBox2.Items.Remove(fileFields(1))
            Next
        End If



    End Sub

Is it possible to do this on users click in a listbox with only the Destination Name showing - the related miles in the row would be deleted also and then I would need to refresh my listboxes and combobox I use on the main form (I can take care of the refreshing of that) I just need to know if its possible to do this in a selectindexchanged event. I started to use a Streamreader, maybe that's not the way to go. Looking for some advanced help on this one.

Heres a shot of what the user will be looking at:

Thanks in advance

Pete


Viewing all articles
Browse latest Browse all 12583

Trending Articles



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