Link to home
Start Free TrialLog in
Avatar of cmdolcet
cmdolcetFlag for United States of America

asked on

VB 2005 how to get a new line of data each time I try to write to a file.

Currently the code give Datavalue1,Datavalue2,.....

and I would like the saved text file to write:

DataValue1
DataValue2

 Private Sub mnuSaveOption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveOption.Click
        Dim Readings As New System.Text.StringBuilder()
        dlgSaveLiveReadingsFile.Filter = "TXT Files (*.txt*)|*.txt"
        Try
            For Each number As Object In Me.lstLiveReadings.Items
                Readings.Append(number)
            Next
            If dlgSaveLiveReadingsFile.ShowDialog = Windows.Forms.DialogResult.OK Then
                My.Computer.FileSystem.WriteAllText(dlgSaveLiveReadingsFile.FileName, Readings.ToString, False)
            End If
        Catch ex As Exception
        End Try
    End Sub

Open in new window

Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Private Sub mnuSaveOption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveOption.Click
        Dim Readings As New System.Text.StringBuilder()
        dlgSaveLiveReadingsFile.Filter = "TXT Files (*.txt*)|*.txt"
        Try
            If dlgSaveLiveReadingsFile.ShowDialog = Windows.Forms.DialogResult.OK Then
                Readings.Append(DataValue1)
                Readings.Append(DataValue2)
                My.Computer.FileSystem.WriteAllText(dlgSaveLiveReadingsFile.FileName, Readings.ToString, False)
            End If
        Catch ex As Exception
        End Try
    End Sub

Open in new window

Avatar of cmdolcet

ASKER

how would I get it to write an infinite number of readings.
Shaun Vermaak,

How would this apply to data row that you dont have any number to so n data rows?
Supose you have a string array values
Dim value() As String = {"Value1", _
                                 "Value2", _
                                 "Value3", _
                                 "Value4"}

Open in new window

Private Sub mnuSaveOption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveOption.Click
        Dim Readings As New System.Text.StringBuilder()
        dlgSaveLiveReadingsFile.Filter = "TXT Files (*.txt*)|*.txt"
        Try
            If dlgSaveLiveReadingsFile.ShowDialog = Windows.Forms.DialogResult.OK Then
                For Each value As String In values
                    Readings.Append(value)
                Next
                My.Computer.FileSystem.WriteAllText(dlgSaveLiveReadingsFile.FileName, Readings.ToString, False)
            End If
        Catch ex As Exception
        End Try
    End Sub

Open in new window

OK, How can I use it with the " Me.lstLiveReadings.Items" code?
Private Sub mnuSaveOption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveOption.Click
        Dim Readings As New System.Text.StringBuilder()
        dlgSaveLiveReadingsFile.Filter = "TXT Files (*.txt*)|*.txt"
        Try
            If dlgSaveLiveReadingsFile.ShowDialog = Windows.Forms.DialogResult.OK Then
                For Each item As ListViewItem In Me.lstLiveReadings.Items 
                    Readings.Append(item.Text)
                Next
                My.Computer.FileSystem.WriteAllText(dlgSaveLiveReadingsFile.FileName, Readings.ToString, False)
            End If
        Catch ex As Exception
        End Try
    End Sub

Open in new window

I get this after my code executes.
Error-Message.PNG
Private Sub mnuSaveOption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSaveOption.Click
        Dim Readings As New System.Text.StringBuilder()
        dlgSaveLiveReadingsFile.Filter = "TXT Files (*.txt*)|*.txt"
        Try
            If dlgSaveLiveReadingsFile.ShowDialog = Windows.Forms.DialogResult.OK Then
                For Each item As String In Me.lstLiveReadings.Items 
                    Readings.Append(item)
                Next
                My.Computer.FileSystem.WriteAllText(dlgSaveLiveReadingsFile.FileName, Readings.ToString, False)
            End If
        Catch ex As Exception
        End Try
    End Sub

Open in new window

So the code above I copied into me environment and ran it and it gives me:

-5.11,L00299-5.11,L00299-5.11,L00299-5.11,L00299
I need it to be:

-5.11,L00299
-5.11,L00299
-5.11,L00299
-5.11,L00299
ASKER CERTIFIED SOLUTION
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial