Search code examples
c#printdialog

C# .Net 3.5 Remove Event added to PrintDialog before add new event


When I click button "Print" my form

private void btnPrint_Click(object sender, EventArgs e)
{
    PrintDialog.Document.PrintPage += (pSender, pE) => PrintEvent(pSender, pE, PrintInfo);
    PrintDialog.Document.Print();
}

PrintDialog is global variable type System.windows.forms.PrintDialog. When I click button "Print" at second time, I want remove PrintEvent added at the first time clicked button before add new PrintEvent. How can I do?


Solution

  • To do it, you must convert lambda to method:

    private void btnPrint_Click(object sender, EventArgs e)
    {
        PrintDialog.Document.PrintPage -= Print;
        PrintDialog.Document.PrintPage += Print;
        PrintDialog.Document.Print();
    }
    
    void Print(object sender, PrintPageEventArgs e)
    (
        //pass info
        PrintEvent(sender, e, PrintInfo)
    )