How to print dialogue ok after delegate?

113 views Asked by At

I have a print dialogue that is triggered from the a button on the binding navigator toolstrip and to give the print dialogue box focus I have created a delegate. My question is how do you trigger the print if dialogue result ok?

This is my code...

    delegate DialogResult ShowPrintDialogue();

    private void trackPrint_Click(object sender, EventArgs e)
    {
        PrintDocument docToPrint = new PrintDocument();
        trackPrintDialog.AllowSomePages = true;
        trackPrintDialog.Document = docToPrint;
        docToPrint.DefaultPageSettings.Landscape = true;
        docToPrint.DocumentName = "Track";

        ShowPrintDialogue spd = new ShowPrintDialogue(trackPrintDialog.ShowDialog);
        this.BeginInvoke(spd);

        if (spd() == DialogResult.OK) // Doesn't work...
        {
            docToPrint.PrintPage += new PrintPageEventHandler(PrintImage);
            docToPrint.Print();
        }
    }
1

There are 1 answers

0
Kcvin On

I'm not exactly sure why you would want to do this. Calling trackPrintDialog.ShowDialog() should show the print dialog and focus it for user interaction. It should also prevent controls beneath the dialog from being changed.

Calling .BeginInvoke means you're calling an asynchronous section of code, meaning it immediately returns and continues to run the code after it. What that means is that since the user isn't, almost instantaneously, clicking OK on the print dialog, the if-statement evaluates spd() to be None and nothing will print.

The PrintDialog is intended to be blocking, so using a delegate to show and focus the print dialog should not done.