C# rinting Function Help

HI Guys,

I am having hastles with a print function that I am trying to put together. First try at this and it seems to be more complicated than I thought. I have a Print function and a PrintPreview function but when running it i get an error. The error seems to be in the DrawAll function and I don't know what it is. The error message I get is "Object reference not set to an Instance of an object"
Below the code I have written for the printing function. Can somebody please have a look at the below and let me know please cause I have now tried everything and I don't know where to go from here.
[CODE]
public partial class Orders : Form
{

private PrintDocument printDoc = new PrintDocument();
private PageSettings pgSettings = new PageSettings();
private PrinterSettings prtSettings = new PrinterSettings();






public Orders()
{
MenuItem fileMenuItem = new MenuItem("&File");
MenuItem filePageSetupMenuItem = new MenuItem("Page Set&up...", new EventHandler(filePageSetupMenuItem_Click));
MenuItem filePrintPreviewMenuItem = new MenuItem("Print Pre&view", new EventHandler(filePrintPreviewMenuItem_Click));
MenuItem filePrintMenuItem = new MenuItem("&Print...", new EventHandler(filePrintMenuItem_Click), Shortcut.CtrlP);

fileMenuItem.MenuItems.Add(filePageSetupMenuItem);
fileMenuItem.MenuItems.Add(filePrintPreviewMenuItem);
fileMenuItem.MenuItems.Add(filePrintMenuItem);
this.Menu = new MainMenu();
this.Menu.MenuItems.Add(fileMenuItem);
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);

InitializeComponent();
}

#region Printing Code

private void filePrintMenuItem_Click(Object sender, EventArgs e)
{
printDoc.DefaultPageSettings = pgSettings;
PrintDialog dlg = new PrintDialog();
dlg.Document = printDoc;
if (dlg.ShowDialog() == DialogResult.OK)
{
printDoc.Print();
}
}
private void filePrintPreviewMenuItem_Click(Object sender, EventArgs e)
{
try
{

PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = printDoc;
dlg.FormBorderStyle = FormBorderStyle.Fixed3D;
dlg.SetBounds(20, 20, this.Width, this.Height);
dlg.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}



}
private void filePageSetupMenuItem_Click(Object sender, EventArgs e)
{
PageSetupDialog pageSetupDialog = new PageSetupDialog();
pageSetupDialog.PageSettings = pgSettings;
pageSetupDialog.PrinterSettings = prtSettings;
pageSetupDialog.AllowOrientation = true;
pageSetupDialog.AllowMargins = true;
pageSetupDialog.ShowDialog();
}

private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)
{
/*e.Graphics.Clip = new Region(e.MarginBounds);
String textToPrint = ".NET Printing is easy";
Font printFont = new Font("Courier New", 12);
int leftMargin = e.MarginBounds.Left;
int topMargin = e.MarginBounds.Top;
e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, leftMargin, topMargin);
*/
DrawAll(e.Graphics);

}
private void DrawAll(Graphics g)
{
[/CODE]
Now I know the error is in the following bit:
[CODE]
RectangleF srcRect = new Rectangle(0, 0, this.BackgroundImage.Width, BackgroundImage.Height);
[/CODE]
[CODE]
int nWidth = printDoc.PrinterSettings.DefaultPageSettings.PaperSize.Width;
int nHeight = printDoc.PrinterSettings.DefaultPageSettings.PaperSize.Height;
RectangleF desct = new Rectangle(0, 0, nWidth, nHeight / 2);

g.DrawImage(this.BackgroundImage, desct, srcRect, GraphicsUnit.Pixel);

float scalex = desct.Width / srcRect.Width;
float scaley = desct.Height / srcRect.Height;

Pen aPen = new Pen(Brushes.Black, 1);

for (int i = 0; i < this.Controls.Count; i++)
{
if (Controls[i].GetType() == this.t1TextBox.GetType())
{
TextBox theText = (TextBox)Controls[i];
g.DrawString(theText.Text, theText.Font, Brushes.Black, theText.Bounds.Left * scalex, theText.Bounds.Top * scaley, new StringFormat());
}
if (Controls[i].GetType() == this.groupBox1.GetType())
{
GroupBox theGroup = (GroupBox)Controls[i];
Rectangle aRect = theGroup.Bounds;
g.DrawRectangle(aPen, aRect.Left * scalex, aRect.Top * scaley, aRect.Width * scalex, aRect.Height * scaley);
}
if (Controls[i].GetType() == this.orderDateDateTimePicker.GetType())
{
DateTimePicker aPicker = (DateTimePicker)Controls[i];
g.DrawString(aPicker.Text, aPicker.Font, Brushes.Black, aPicker.Bounds.Left * scalex, aPicker.Bounds.Top * scaley, new StringFormat());
}
if (Controls[i].GetType() == this.comboBox1.GetType())
{
ComboBox theCombo = (ComboBox)Controls[i];
Rectangle cRect = theCombo.Bounds;
g.DrawRectangle(aPen, cRect.Left * scalex, cRect.Top * scaley, cRect.Width * scalex, cRect.Height * scaley);
}

}
}
[/CODE]

Comments

  • hi
    is hard for me to find the bug just by looking at code, it is easier for me to use the debugger.
    I do not enderstend in witch line you have the error
    try to use some try-catch and do some checks like:
    if(object==null)
    MessageBox.Show("some identification message");

    if you didn't solve this yet you can email me and i will try to help you
    simion314
  • Hi Simion. I managed to rsolve this one. I was missing this in my code.
    BackgroundImage = new Bitmap(Width, Height);
    Now, for some strange reason all the text boxes don't seem to print and I think it is because they re sitting within a group box. Do you know how I get them to print? When I do a PrintPreview they don't show on the document.


    : hi
    : is hard for me to find the bug just by looking at code, it is easier
    : for me to use the debugger.
    : I do not enderstend in witch line you have the error
    : try to use some try-catch and do some checks like:
    : if(object==null)
    : MessageBox.Show("some identification message");
    :
    : if you didn't solve this yet you can email me and i will try to help
    : you
    : simion314

  • hi
    i am sorry but i do not know how to use printing, try to loop to all containers in the form and draw all controls.Try search at code project web site.
    simion314
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories