Cobbling together some code I have spent most of the day trying to implement printing into Recipe King only to get the error:
An unhandled exception of type 'System.ArgumentNullException' occurred in system.drawing.dll
Additional information: Value cannot be null.based upon the following code:
Code:
//PrintPage event raised for each page to be printed
private void objPrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
float fltYPosition;
float fltXPosition;
//represents left margin
float fltLeftMargin= e.MarginBounds.Left;
//represents top margin
float fltTopMargin=e.MarginBounds.Top;
string strLine="";
//iterate over the form, printing each control
foreach(Control objControl in this.Controls)
{
strLine=objControl.Text;
//set string position relative to page margins
fltXPosition=fltLeftMargin + objControl.Location.X;
fltYPosition=fltTopMargin + objControl.Location.Y;
//draw text in graphic object
e.Graphics.DrawString(strLine, m_objFont, Brushes.Black, fltXPosition, fltYPosition);
}//end foreach
//draw a line around form
e.Graphics.DrawRectangle(Pens.Black, fltLeftMargin, fltTopMargin, this.Width, this.Height-60);
//indicates there are no more pages to print
e.HasMorePages=false;
}
private void menuPrint_Click(object sender, System.EventArgs e)
{
//print document
//create new object to assist in printing
PrintDocument objPrintDocument = new PrintDocument();
//add PrintPage handler
objPrintDocument.PrintPage+= new PrintPageEventHandler(objPrintDocument_PrintPage);
//if no printer is installed, display error message
if(PrinterSettings.InstalledPrinters.Count==0)
{
MessageBox.Show("No Printers installed. You must have a printer installed to print", "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//print the document
objPrintDocument.Print();
//MessageBox.Show( "We apoligize. This function has not been activated yet. This will be activated in future version of this program","Message", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
Freak IT!!!
*sigh*