locked
Right hand justifying print text RRS feed

  • Question

  • I'm trying to print out some arrays of data, arranged in columns, but am having a few problems:

    1) I don't think that I'm calculating the x coordinate correctly for each column

    2) The text won't right hand justify

    Sometimes, I get the code fairly close to working, and then make what I think is the final change, but it seems to make other things worse. I'm a hobbyist,  just learning VB for my own use, so I know that my code won't bring tears of joy to a professional coder!

    The 1st column is the year (4 digits) and subsequent ones are all numerical, of varying amounts, so I'm formatting them and then padding the left side with blanks to give a constant string length to help with the calulation of the x coordinate

    My code looks like this:

    PrintString = CInt(yyear(i))
    ColumnWidth = e.Graphics.MeasureString(PrintString, prFont).Width
    e.Graphics.DrawString(PrintString, prFont, Brushes.Black, Xpos, Ypos)

    Xpos = Xpos + ColumnWidth
    PrintString = CInt(RegIncome(i))
    PrintString = FormatNumber(PrintString, 0)
    PrintString = PrintString.PadLeft(8)
    ColumnWidth = e.Graphics.MeasureString(PrintString, prFont).Width

    e.Graphics.DrawString(PrintString, prFont, Brushes.Black, Xpos, Ypos)

    Xpos = Xpos + ColumnWidth
    PrintString = CInt(Withdrawal(i))
    PrintString = FormatNumber(PrintString, 0)
    PrintString = PrintString.PadLeft(8)
    ColumnWidth = e.Graphics.MeasureString(PrintString, prFont).Width

    e.Graphics.DrawString(PrintString, prFont, Brushes.Black, Xpos, Ypos)

    Thanks, in advance, for any help

    Ray

    Saturday, April 14, 2012 8:21 PM

Answers

  • You need to keep track of two separate items - the column start position and the print start position.  The column start is updated for the width of the whole column, and the print position is calculated for each item from the start of the current column,m the width of that column, and the width of the print string.

    ColumnStart = 250 ' first column left margin
    ColumnWidth = 300 ' Could be different for each column

    PrintString = CInt(yyear(i))
    PrintWidth = e.Graphics.MeasureString(PrintString, prFont).Width
    XPos = ColumnStart + ColumnWidth - PrintWidth ' right justify
    e.Graphics.DrawString(PrintString, prFont, Brushes.Black, Xpos, Ypos)

    ColumnStart += ColumnWidth 'next column
    PrintString = CInt(RegIncome(i))
    PrintString = FormatNumber(PrintString, 0)
    PrintWidth = e.Graphics.MeasureString(PrintString, prFont).Width
    XPos = ColumnStart + ColumnWidth - PrintWidth
    e.Graphics.DrawString(PrintString, prFont, Brushes.Black, Xpos, Ypos)

    ColumnStart += ColumnWidth 'next column
    PrintString = CInt(Withdrawal(i))
    PrintString = FormatNumber(PrintString, 0)
    PrintWidth = e.Graphics.MeasureString(PrintString, prFont).Width
    XPos = ColumnStart + ColumnWidth - PrintWidth
    e.Graphics.DrawString(PrintString, prFont, Brushes.Black, Xpos, Ypos)

    • Marked as answer by Ray_M_G Sunday, April 15, 2012 2:38 PM
    Saturday, April 14, 2012 10:05 PM

All replies

  • Use a DrawString overload that takes a rectangle and a StringFormat.  That lets you draw text aligned near, centered or far in the rectangle, both horizontally and vertically.
    Saturday, April 14, 2012 9:17 PM
  • You need to keep track of two separate items - the column start position and the print start position.  The column start is updated for the width of the whole column, and the print position is calculated for each item from the start of the current column,m the width of that column, and the width of the print string.

    ColumnStart = 250 ' first column left margin
    ColumnWidth = 300 ' Could be different for each column

    PrintString = CInt(yyear(i))
    PrintWidth = e.Graphics.MeasureString(PrintString, prFont).Width
    XPos = ColumnStart + ColumnWidth - PrintWidth ' right justify
    e.Graphics.DrawString(PrintString, prFont, Brushes.Black, Xpos, Ypos)

    ColumnStart += ColumnWidth 'next column
    PrintString = CInt(RegIncome(i))
    PrintString = FormatNumber(PrintString, 0)
    PrintWidth = e.Graphics.MeasureString(PrintString, prFont).Width
    XPos = ColumnStart + ColumnWidth - PrintWidth
    e.Graphics.DrawString(PrintString, prFont, Brushes.Black, Xpos, Ypos)

    ColumnStart += ColumnWidth 'next column
    PrintString = CInt(Withdrawal(i))
    PrintString = FormatNumber(PrintString, 0)
    PrintWidth = e.Graphics.MeasureString(PrintString, prFont).Width
    XPos = ColumnStart + ColumnWidth - PrintWidth
    e.Graphics.DrawString(PrintString, prFont, Brushes.Black, Xpos, Ypos)

    • Marked as answer by Ray_M_G Sunday, April 15, 2012 2:38 PM
    Saturday, April 14, 2012 10:05 PM
  • Thanks Acamar.... it worked.

    I changed the column width a few times until the printout looked nice. Pity that it can't be calculated though.

    Thanks again.....  Ray

    Sunday, April 15, 2012 2:39 PM
  • "Pity that it can't be calculated though."

    I guess you missed my post.

    Sunday, April 15, 2012 2:49 PM
  • No John, I did see your post, but Acamar provided some sample code that I could copy and paste into my program. I'm very inexperienced with Visual Basic, and I went for the easiest solution.

    No doubt the solution that you suggested is technically more correct, but the whole VB printing technology is proving much more complicated than I thought. I'm from the old school that remembers when printing was simply sending a text string to a printer, one line at a time.

    Thanks for your help though.....  as I learn more about VB I can try to use your suggested solution

    Sunday, April 15, 2012 4:00 PM
  • Just accept it for what it is.  You're cursing the darkness because you don't want to turn on a light.

    "I'm from the old school that remembers when printing was simply sending a text string to a printer, one line at a time."

    If that's the way you want to do it then use the Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6.Printer.

    Sunday, April 15, 2012 4:40 PM
  • I changed the column width a few times until the printout looked nice. Pity that it can't be calculated though.

    I'm not sure what you mean by 'calculated', but there are several ways to right justify code into columns, and they all involve some sort of calculation. The procedure you are using involves the most explicit calculation, but the alternative that has been suggested does the same sort of thing  - it's just already coded in the methods that position the string within the rectangle that defines the column.   The problem with the code you are using is that it needs to be rewritten for each variation in your format - if you use a rectangle definition for the required print position then you end up using the same code (with different rectangles) for everything.  For three columns, that's not an issue.  For more than that, or if the number of columns could vary, it becomes important.

    Sunday, April 15, 2012 8:57 PM
  • Hi,

    Acamar solution is right and provided a truthful example.

    Monday, April 16, 2012 4:56 AM