I have written an application in C# but need help with formatting/ alignment. The code is shortened so as to focus on the problem. I'm printing transactions to a receipt printer but need to right align the net amounts.

Example
Item Amt Cost Net Amt
USD 30 66.50 1995.00
USD 1400 66.80 93520.00

PrintDocument pd = new PrintDocument();
pd.Print();

private void PrintPageEvent( object sender, PrintPageEventArgs ev)
{
float topMargin = ev.PageBounds.Top;
float leftMargin = ev.PageBounds.Left;

// where transaction is a string as indicated above
string trans = String.Format("{0}\t{1}\t{2:F2}\t{3:N}", item, Amt, Cost, NetAmt);
Graphics g = ev.Graphics;
g.DrawString(trans, printFont, Brushes.Black, leftMargin, ...);
}

Why don't you use a monospaced font (for example Courier) and then write a small function that will pad your string with spaces on its left so that it becomes right aligned...

I always do this when writing applications that need to print on receipt printers.

Let me know if you need more clarifications.
Cheers :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.