Chris Rutherford
Chris Rutherford

Reputation: 1672

C# Auto Generated Methods. How to add additional parameters

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()?

Upvotes: 2

Views: 245

Answers (1)

Emre Kabaoglu
Emre Kabaoglu

Reputation: 13146

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);

}

Upvotes: 3

Related Questions