user4113185
user4113185

Reputation:

printing a receipt in C#

Hi I'm trying to print a bill for a certain transaction and i've done it but the subtotal is printing over the total line... your help is appreciated. My Code below

    offset = offset + 20;
        graphics.DrawString("Total : ".PadRight(40) + total.Text, font, new SolidBrush(Color.Black), startX, startY);

        graphics.DrawString("SubTotal : ".PadRight(40) + subtotal.Text, font, new SolidBrush(Color.Black), startX, startY);

Upvotes: 2

Views: 463

Answers (1)

Nico
Nico

Reputation: 12683

The line graphics.DrawString("SubTotal : ".PadRight(40) + subtotal.Text, font, new SolidBrush(Color.Black), startX, startY); is set (via startY) to write at the same y co-ordinate as the previous line.

Best to increment the startY by the font height. Something like:

offset = offset + 20;
graphics.DrawString("Total : ".PadRight(40) + total.Text, font, new SolidBrush(Color.Black), startX, startY);

startY += font.Height;
graphics.DrawString("SubTotal : ".PadRight(40) + subtotal.Text, font, new SolidBrush(Color.Black), startX, startY);

This increments the startY by the font.Height amount which by MSDN is

Gets the line spacing of this font.

Remarks:

The line spacing is the vertical distance between the base lines of two consecutive lines of text. Thus, the line spacing includes the blank space between lines along with the height of the character itself.

Upvotes: 1

Related Questions