Results 1 to 3 of 3

Thread: [RESOLVED] One line and formatted

  1. #1

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Resolved [RESOLVED] One line and formatted

    Following on from a previous thread for saving a file I have the following, which places each item on a new line.
    Code:
        Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
            Dim path As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
            Dim Filename As String = "\BowlingLog.txt" 'you can use a Textbox for Filename
            path = String.Concat(path, Filename)
            File.AppendAllLines(path, {lblHome.Text, tbHome1.Text, tbHome2.Text, tbHome3.Text, tbHome4.Text, tbTotalHome.Text, tbHomePoints.Text})
            File.AppendAllLines(path, {tbAway.Text, tbAway1.Text, tbAway2.Text, tbAway3.Text, tbAway4.Text, tbTotalAway.Text, tbAwayPoints.Text})
            Call SaveFileForAppend(path)
            Process.Start(path)
        End Sub
    What I want is to get each set on one line so I went with this, which works.

    Code:
        Private Sub SaveFileForAppend(ByVal Filename As String)
            Dim sw As System.IO.StreamWriter
    
            Try
                'add Text to the File E:\LogF.txt
                If System.IO.File.Exists(Filename) Then
                    sw = System.IO.File.AppendText(Filename)
    
                Else
                    'if E:\LogF.txt does not exist then
                    'create it and write to a new File
                    sw = System.IO.File.CreateText(Filename)
                End If
    
                With sw
                    .Write("Team" & "     " & "Rink1" & "     " & "Rink2" & "     " & "Rink3" & "     " & "Rink4" & "     " & "Total Score" & "     " & "Total Points" & System.Environment.NewLine)
    
                    .Write(lblHome.Text & "     " & tbHome1.Text & "     " & tbHome2.Text & "     " & tbHome3.Text & "     " & tbHome4.Text & "     " & tbTotalHome.Text & "     " & tbHomePoints.Text & System.Environment.NewLine)
                    .Write(tbAway.Text & "     " & tbAway1.Text & "     " & tbAway2.Text & "     " & tbAway3.Text & "     " & tbAway4.Text & "     " & tbTotalAway.Text & "     " & tbAwayPoints.Text & System.Environment.NewLine)
                    .Flush()
                    .Close()
                End With
            Catch ex As IOException
                MessageBox.Show(ex.Message.ToString())
            End Try
        End Sub
    But saves it like this
    Code:
    Team     Rink1     Rink2     Rink3     Rink4     Score     Points
    Llansantffraid     22     25     25     19     91     5
    Opposition     23     22     26     15     86     2
    It would be better if it could be saved like this
    Code:
    Team             Rink1   Rink2  Rink3  Rink4  Score  Points
    
    Llansantffraid     23     25     22     26      96     7
    Opposition         21     19     21     22      83     0
    The question, Is there a way to save the text better formatted?
    Learning is a never ending subject.

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: One line and formatted

    Hi,

    play around with this

    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim s As String = New String(" "c, 60)
            s = MidString(s, "Bauer GmbH", 1)
            s = MidString(s, "12345", 19)
            s = MidString(s, "Schrobenhausen", 31)
            Debug.Print(s & "*")
        End Sub
    
        Public Function MidString(ByVal BaseString As String, ByVal ReplaceString As String, ByVal StartIndex As Integer) As String
    
            Dim i As Integer = ReplaceString.Length
            Dim s As String = BaseString.Substring(0, StartIndex - 1) & _
                              ReplaceString & _
                              BaseString.Substring(StartIndex + i - 1)
            Return s
        End Function
    
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim firma As String = "Bauer GmbH"
            Dim plz As String = "12345"
            Dim ort As String = "Schrobenhausen"
            Dim test As String = String.Format("{0,-15} | {1,5} | {2,-15}", firma, plz, ort)
            Debug.Print(test)
        End Sub
    
        
        Private Sub SaveFileForAppend(ByVal Filename As String)
            Dim sw As System.IO.StreamWriter
    
            Try
                'add Text to the File E:\LogF.txt
                If System.IO.File.Exists(Filename) Then
                    sw = System.IO.File.AppendText(Filename)
    
                Else
                    'if E:\LogF.txt does not exist then
                    'create it and write to a new File
                    sw = System.IO.File.CreateText(Filename)
                End If
                With sw
                    Dim firma As String = "Bauer GmbH"
                    Dim plz As String = "12345"
                    Dim ort As String = "Schrobenhausen"
                    Dim test As String = String.Format("{0,-15} | {1,5} | {2,-15}", firma, plz, ort)
    
                    .Write(test & System.Environment.NewLine)
                    '.Write(TextBox2.Text & System.Environment.NewLine)
                    '.Write(TextBox3.Text & System.Environment.NewLine)
                    .Flush()
                    .Close()
                End With
            Catch ex As IOException
                MessageBox.Show(ex.Message.ToString())
            End Try
        End Sub
    HTH
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Re: One line and formatted

    Thanks again Chris, will copy it in to a new project and play around, once I see what does what I'll import it in to my project.
    I'll mark this as 'Solved' and if I need to come back in a couple of days I'll start a new thread.
    Learning is a never ending subject.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width