2

Working on a Receipt Printer app for a local business. It's technically a re-write, and I'm approaching this as a complete refactor.

The original code looks like this:

private void btn_print_Click(object sender, EventArgs e)
        {

        PrintDialog pd = new PrintDialog();
        PrintDocument doc = new PrintDocument();
        pd.Document = doc;
        doc.PrintPage += new PrintPageEventHandler(pd_printpage);
        DialogResult res = pd.ShowDialog();
        if (res == DialogResult.OK)
        {
            doc.DefaultPageSettings.Landscape = false;
            doc.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Env10", 4, 8);
            doc.Print();
        }

    }
    private void pd_printpage(object sender, PrintPageEventArgs e)
    {
        Graphics gfx = e.Graphics;
        SolidBrush blk = new SolidBrush(Color.Black);
        Font courier = new Font("Courier New", 12);
        Font lucidia = new Font("Lucida Sans", 12);
        Font lucidaplus = new Font("Lucida Sans", 18);
        StringFormat fmt = new StringFormat(StringFormatFlags.DirectionVertical);

        /*****  Create Object for Reciept  *****/
        Recpt rcpt = new Recpt();
        rcpt.Contrib = new ContInfo();
        rcpt.Pri = new Address();
        rcpt.Alt = new Address();

//--- Remainder of function omitted.

Here is a better factored version that I'm currently working on.

static class Printality
    {
        private static SolidBrush Blk = new SolidBrush(Color.Black);
        private static Font CourierBody = new Font("Courier New", 12);
        private static Font LucidaBody = new Font("Lucida Sans", 12);
        private static Font LucidaHeader = new Font("Lucida Sans", 18);
        private static StringFormat fmt;

        public static void PrintReciept(Receipt _rec)
        {
            PrintDialog pd = new PrintDialog();
            PrintDocument doc = new PrintDocument();
            doc.PrintPage += new PrintPageEventHandler(pd_printPage);
        }

        private static void pd_printPage(object sender, PrintPageEventArgs e)
        {
            Graphics gfx = e.Graphics;
            fmt = new StringFormat(StringFormatFlags.DirectionVertical);

        }
    }

the big question is this: I'm passing a Receipt object to the function printReceipt() how should I also pass it to pd_printPage()?

1
  • Try : static class Printality : PrintDocument Commented Jan 19, 2018 at 18:28

1 Answer 1

3

If I understood your question clearly, you want to pass parameter to event.

doc.PrintPage += (sender, e) => pd_printPage(sender, e, _rec);

private static void pd_printPage(object sender, PrintPageEventArgs e, Receipt rec)
{
    Graphics gfx = e.Graphics;
    fmt = new StringFormat(StringFormatFlags.DirectionVertical);

}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah! You've understood exactly! I've been in JavaScript land too much. I didn't realize C# had that kind of functionality! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.