Aligning text in columns RRS feed

  • Question

  • I have user-selectable font for the string, but have not been able to come up with a way to align all the columns I am printing.

    I've been trying to use String.PadRight on the items, but it is not keeping them aligned.

    Any ideas or points for the right direction?

    Wednesday, June 14, 2017 5:25 PM

All replies

  • Hello,

    First off we don't know the context you are working in. No matter, when attempting to align data in columns say in a report you would use the functionality of the class or control to do styling, not padding of data.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, June 14, 2017 5:39 PM
  • I have user-selectable font for the string, but have not been able to come up with a way to align all the columns I am printing.

    I've been trying to use String.PadRight on the items, but it is not keeping them aligned.

    Any ideas or points for the right direction?


    I had similar requirements in an old project. I ended up restricting the user to mono spaced fonts (of which there weren't that many), and all alignments were straightforward.

    If you use proportional spaced fonts, then you would need to search further. I couldn't find a way to do it with proportional fonts when I had that issue.

    Regards Les, Livingston, Scotland

    Wednesday, June 14, 2017 6:07 PM
  • Hi Chris, 

    Yea it is not easy to find a solution for your problem with with Visual Studio. 

    A pity is the solutions for it are not consistent in Visual Studio, therefore we need the version you are using. 

    It is not my favorite topic by the way, but maybe it helps you on the road. 

    Here the page for VS 2017

    Be aware I know 0,0% about it. Just now that I see that many have problems to find it. 


    Wednesday, June 14, 2017 6:54 PM
  • This prints columns of text and numbers that are right justified.

    If you want something else please show a picture of exactly what you want even a data sample.

    Imports System.Drawing.Printing
    'prints columns of text on multiple pages with printpreview right justified
    Public Class Form2
        Private WithEvents PrintDocument1 As PrintDocument = New PrintDocument
        Private PrintPreviewDialog1 As New PrintPreviewDialog
        Private PagesPrinted, LInesPrinted As Integer
        Structure MyReading
            Public Name As String
            Public Distance As Single
            Public ReqFlow As Single
            Public ActFlow As Single
            Public PSI As Single
        End Structure
        Private MyReadings As New List(Of MyReading)
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'create some data
            Dim areading As MyReading
            For i = 1 To 100
                areading.Name = "R - " & i.ToString
                areading.Distance = 100 + (20 * i)
                areading.ReqFlow = CSng(i * 0.5)
                areading.ActFlow = CSng(i * 0.4)
                areading.PSI = CSng(20 + (i * 0.01))
            PrintPreviewDialog1.Document = PrintDocument1
        End Sub
        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim f As Font = New Font("arial", 12)
            Dim printingdone As Boolean
            With e.Graphics
                Dim t As String = ""
                Dim sz As New SizeF
                Dim x As Integer
                Dim dx = e.MarginBounds.Right - 600
                Dim texth As Single = .MeasureString("T", f).Height
                PagesPrinted += 1
                'draw the heading and page number
                .DrawString("Position", f, Brushes.Black, dx, e.MarginBounds.Top)
                .DrawString("Distance", f, Brushes.Black, dx + 100, e.MarginBounds.Top)
                .DrawString("ReqFlow", f, Brushes.Black, dx + 200, e.MarginBounds.Top)
                .DrawString("ActFlow", f, Brushes.Black, dx + 300, e.MarginBounds.Top)
                .DrawString("PSI", f, Brushes.Black, dx + 400, e.MarginBounds.Top)
                .DrawString("Page " & PagesPrinted.ToString, f, Brushes.Black, dx + 500, e.MarginBounds.Top)
                For y = e.MarginBounds.Top + (2 * texth) To e.MarginBounds.Bottom Step texth
                    'draw a data line
                    t = MyReadings(LInesPrinted).Name
                    sz = .MeasureString(t, f)
                    x = CInt(dx + 50 - sz.Width)
                    .DrawString(t, f, Brushes.Black, x, y)
                    t = MyReadings(LInesPrinted).Distance.ToString
                    sz = .MeasureString(t, f)
                    x = CInt(dx + 150 - sz.Width)
                    .DrawString(t, f, Brushes.Black, x, y)
                    t = MyReadings(LInesPrinted).ReqFlow.ToString
                    sz = .MeasureString(t, f)
                    x = CInt(dx + 250 - sz.Width)
                    .DrawString(t, f, Brushes.Black, x, y)
                    t = MyReadings(LInesPrinted).ActFlow.ToString
                    sz = .MeasureString(t, f)
                    x = CInt(dx + 350 - sz.Width)
                    .DrawString(t, f, Brushes.Black, x, y)
                    t = MyReadings(LInesPrinted).PSI.ToString
                    sz = .MeasureString(t, f)
                    x = CInt(dx + 450 - sz.Width)
                    .DrawString(t, f, Brushes.Black, x, y)
                    LInesPrinted += 1
                    If LInesPrinted >= MyReadings.Count Then
                        printingdone = True
                        Exit For
                    End If
            End With
            If printingdone Then
                e.HasMorePages = False
                PagesPrinted = 0
                LInesPrinted = 0
                e.HasMorePages = True
            End If
        End Sub
    End Class

    Wednesday, June 14, 2017 7:46 PM
  • Hi ChrisF8657,

    Based on your description. you want to align all the columns when you print, I suggest you to use Graphics.DrawString Method (String, Font, Brush, RectangleF, StringFormat) to set left, right. center.

    The code below is to set content align left.

     Dim drawFormat As New StringFormat
            drawFormat.Alignment = StringAlignment.Center
            e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black,
                e.MarginBounds, drawFormat)

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, June 15, 2017 6:08 AM