Handle Errors

The LinePrinter has 2 levels of event notification that can be communicated to the application when some condition is threatening the proper completion of a report:

Not all printers can return enough information to the computer to allow for warnings, and in such cases will only generate errors that throw the LinePrinterException. Likewise, even on printers that are capable of issuing warnings, if the application fails to register a WarningMessageEvent handler, then the warnings will automatically be promoted to errors. For example, printers capable of NPCP printing can also issue warnings, whereas most serial and IR printers will only issue errors.

WarningMessageEventEx Event

The StatusMessageTypes are defined as:

public static event WarningMessageEventHandler WarningMessageEventEx;

When a WarningMessageEventEx is fired, it passes as an argument a WarningMessageArgs object. This class has two significant elements:

DeviceType
This field contains a string that is the port type that is being printed to, such as "NPCP."

ErrorCode
This field contains the error number unique to the DeviceType that describes the type of error.

Notes

If you call the Cancel() method from within the WarningMessageEventEx handler, it has the effect of promoting the warning to an error and triggering a LinePrinterException.  If you return without calling Cancel(), the report will continue from where it left off.

An example of registering a WarningMessageEventEx handler:

lp.WarningMessageEventEx += new LinePrinter.WarningMessageEventHandler(WarningHandler);

An example of a simple WarningMessageEventEx handler:

public void WarningHandler(object source, LinePrinter.WarningMessageArgs Error)
{
    string temp;
    DialogResult result;
    temp = string.Format("{0} from {1} Printer \n\r\n\r Fix problem and continue
        printing?",NPCPErrorStr(Error.ErrorCode),Error.DeviceType);
    result = MessageBox.Show(temp, "PRINTER WARNING", MessageBoxButtons.YesNo,
        MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
    if (result == DialogResult.No) lp.Cancel();
}

The static version of this event (WarningMessageEvent) has been obsoleted, and may not be supported in future versions of this assembly.

More Information

Next ».NET Examples and Source Code

Back « Print Routine: Clean Up Resources