Formatting Columns in Text File (StreamWriter for VB.NET)

Ok, i need to format columns in a simple text file that is then sent to a PrintPreviewDialog ...but my columns will not line up.  I wrote a custom 'spacer' ... then I tried the StreamWriter's built in formatting ... it WILL NOT line up.  The current code is below, and the output.

Dim TempFile As New System.IO.StreamWriter("temp")
        TempFile.WriteLine("Customer: " & CustomerName)
        TempFile.WriteLine("Order #: " & OrderNumber)
        TempFile.WriteLine("{0,15}{1,15}{2,15}{3,15}{4,15}{5,15}", "Quantity", "Size", "Monogram", "Pocket", "Price Each", "Total Price")

        For I As Integer = 0 To 140


        For Each Order As OrderEntry In OrderEntries

            If Order.Quantity <> 0 Then

                TempFile.Write("{0,15}", CStr(Order.Quantity))
                TempFile.Write("{0,15}", Order.Size)

                If Order.Monogram Then
                    TempFile.Write("{0,15}", "YES")
                    TempFile.Write("{0,15}", "NO")
                End If

                If Order.Pocket Then
                    TempFile.Write("{0,15}", "YES")
                    TempFile.Write("{0,15}", "NO")
                End If

                TempFile.Write("{0,15}", Order.Price.ToString("C"))
                TempFile.WriteLine("{0,15}", Order.ExtendedPrice.ToString("C"))
            End If


Open in new window

Who is Participating?
Paul_Harris_FusionConnect With a Mentor Commented:
It is virtually impossible to line things up attractively in this way due to different spacing in font sizes etc.

The only way you could do it is to use a fixed width font such as courier new in your viewer.

However, this is likely to be ugly.

The invoice you show is clearly doing something a biit more sophistiated than displaying a raw text file since it includes underlining.

I would suggest you look at writing to a delimited file or XML and then put the layout logic for this in whatever is drawing your invoice.   You could implement precise columns there.
jigdogAuthor Commented:
Luckily, as this is not a professional/paid project, attractiveness is not an issue.  Your explanation is what I needed, and 'Courier New' allowed me to get the alignment correct.  Thanks so much!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.