How do determine if printdialog successful

Jordan Halgunseth 1 Reputation point
2023-03-08T17:07:06.1766667+00:00

How do i tell if print was successful.

I am using this code, I works after failed attempt creates error.

   string printerinUse = pDialog.PrinterSettings.PrinterName;

                var server = new LocalPrintServer();

                PrintQueue queue = server.GetPrintQueue(printerinUse);

                MessageBox.Show("printer " + printerinUse +" is   " + queue.IsOffline.ToString());

                //various properties of printQueue
                bool isOutOfPaper = queue.IsOutOfPaper;
                bool isOffLine = queue.IsOffline;
                bool isPaperJam = queue.IsPaperJammed;
                bool isNotResponding = queue.IsInError;

                MessageBox.Show("printer " + printerinUse + " is   " + isNotResponding.ToString());

             

                if (isOffLine)
                {

                    MessageBox.Show("Printer is offline");
                    return;

                }
                if (isOutOfPaper)
                {

                    MessageBox.Show("Printer is out of paper");
                    return;

                }
                if (isPaperJam)
                {

                    MessageBox.Show("Printer has a paper jam");
                    return;

                }
                if (isNotResponding)
                {

                    MessageBox.Show("Printer is in error");
                    return;

                }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde 28,311 Reputation points MVP
    2023-03-08T23:54:55.31+00:00

    Hello @Jordan Halgunseth ,

    I checked the documentation and it seems the dialog part can be separated from the actual printing.

    Once printing is started and/or ends:

    using System.Drawing.Printing;
    using System.Windows.Forms;
    
    namespace WinFormsApp1
    {
        public partial class Form1 : Form
        {
            PrintDocument docToPrint;
    
            public Form1()
            {
                InitializeComponent();
    
                docToPrint = new PrintDocument();
    
                docToPrint.BeginPrint += DocToPrint_BeginPrint;
                docToPrint.PrintPage += DocToPrint_PrintPage;
                docToPrint.EndPrint += DocToPrint_EndPrint;
            }
    
            private void DocToPrint_BeginPrint(object sender, PrintEventArgs e)
            {
                // now you know printing starts
            }
    
            private void DocToPrint_PrintPage(object sender, PrintPageEventArgs e)
            {
                // now you know it's printing
            }
    
            private void DocToPrint_EndPrint(object sender, PrintEventArgs e)
            {
                // now you know printing ends
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                printDialog1.Document = docToPrint;
    
                DialogResult result = printDialog1.ShowDialog();
    
                // If the result is OK then print the document.
                if (result == DialogResult.OK)
                {
                    docToPrint.Print();
                }
            }
        }
    }
    
    

    You need to add your own logic to connect the three kinds of events together and make a decision.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    0 comments No comments