472,122 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

Printing an Form

Hi

I have designed a form containing data and I want to print that form and the
data as it appears in the Windows Form. I am a beginner with C# and I have
read several "help"s on PrintPage, PrintDialog, etc. but none seem to address
the issue of printing directly from the form. They all talk about printing
from a text file.

Can someone tell me how to print the form and maybe direct me on which to
use PrintPage, PrintDialog....which?

Thanks
D
Nov 17 '05 #1
4 2705
I can give you some generic information. It depends on what version of
VS you're using. You need to "take a picture" of the form, and pass
that to the drawing object when you print. With VS 2005, there is
actually a copy function you can use. With older versions you may need
to import GDI32 and make calls to that.

This is the code I use to print a form:

protected void PrintActiveForm(object sender, EventArgs e)
{
ToolStripMenuItem MenuItem=(ToolStripMenuItem)sender;
//MessageBox.Show(MenuItem.Name);
PrintDialog PD = new PrintDialog();
PrinterSettings PS = new PrinterSettings();
PrintDocument PrtDoc = new PrintDocument();

_ActiveFormImage = SetSeg_Library.SetCapture.Form(this);

PrtDoc.PrintPage += new PrintPageEventHandler(PrtDoc_PrintPage);
PD.Document = PrtDoc;
PD.ShowDialog();
PrtDoc.Print();

}

void PrtDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(this._ActiveFormImage, 0, 0);
}

CsharpNewcommer wrote:
Hi

I have designed a form containing data and I want to print that form and the
data as it appears in the Windows Form. I am a beginner with C# and I have
read several "help"s on PrintPage, PrintDialog, etc. but none seem to address
the issue of printing directly from the form. They all talk about printing
from a text file.

Can someone tell me how to print the form and maybe direct me on which to
use PrintPage, PrintDialog....which?

Thanks
D

Nov 17 '05 #2
Thanks Gary,

I am using VS 2003. I will tryyour suggestion.

Thanks
D.

"Gary Holbrook" wrote:
I can give you some generic information. It depends on what version of
VS you're using. You need to "take a picture" of the form, and pass
that to the drawing object when you print. With VS 2005, there is
actually a copy function you can use. With older versions you may need
to import GDI32 and make calls to that.

This is the code I use to print a form:

protected void PrintActiveForm(object sender, EventArgs e)
{
ToolStripMenuItem MenuItem=(ToolStripMenuItem)sender;
//MessageBox.Show(MenuItem.Name);
PrintDialog PD = new PrintDialog();
PrinterSettings PS = new PrinterSettings();
PrintDocument PrtDoc = new PrintDocument();

_ActiveFormImage = SetSeg_Library.SetCapture.Form(this);

PrtDoc.PrintPage += new PrintPageEventHandler(PrtDoc_PrintPage);
PD.Document = PrtDoc;
PD.ShowDialog();
PrtDoc.Print();

}

void PrtDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(this._ActiveFormImage, 0, 0);
}

CsharpNewcommer wrote:
Hi

I have designed a form containing data and I want to print that form and the
data as it appears in the Windows Form. I am a beginner with C# and I have
read several "help"s on PrintPage, PrintDialog, etc. but none seem to address
the issue of printing directly from the form. They all talk about printing
from a text file.

Can someone tell me how to print the form and maybe direct me on which to
use PrintPage, PrintDialog....which?

Thanks
D

Nov 17 '05 #3
Hey Gary,

Thanks for the code. As I said in my earlier reply I'm using VS2003 and I
guess I have to import that "gdi32.dll" but using your example I'm not sure
how to do that other than it requires "static extern". This is a little over
my head at this point.

Anyway I tried your code and I seem to be missing some key points. For one
I'm using the click event of a button so I don't think I need:
"ToolStripMenuItem MenuItem=(ToolStripMenuItem)sender;"
MessageBox.Show(MenuItem.Name);
Two, don't I need to call PrintActiveForm() and PrtDoc() ? And three, these
are some of the build errors I'm getting:

'TestDB.fclsPastInfo' does not contain a definition for '_ActiveFormImage'

'The name '_ActiveFormImage' does not exist in the class or namespace
'TestDB.fclsPastInfo'

The type or namespace name 'PrtDoc' could not be found (are you missing a
using directive or an assembly reference?)

The type or namespace name 'PrtDoc' could not be found (are you missing a
using directive or an assembly reference?)
D.

"Gary Holbrook" wrote:
I can give you some generic information. It depends on what version of
VS you're using. You need to "take a picture" of the form, and pass
that to the drawing object when you print. With VS 2005, there is
actually a copy function you can use. With older versions you may need
to import GDI32 and make calls to that.

This is the code I use to print a form:

protected void PrintActiveForm(object sender, EventArgs e)
{
ToolStripMenuItem MenuItem=(ToolStripMenuItem)sender;
//MessageBox.Show(MenuItem.Name);
PrintDialog PD = new PrintDialog();
PrinterSettings PS = new PrinterSettings();
PrintDocument PrtDoc = new PrintDocument();

_ActiveFormImage = SetSeg_Library.SetCapture.Form(this);

PrtDoc.PrintPage += new PrintPageEventHandler(PrtDoc_PrintPage);
PD.Document = PrtDoc;
PD.ShowDialog();
PrtDoc.Print();

}

void PrtDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(this._ActiveFormImage, 0, 0);
}

CsharpNewcommer wrote:
Hi

I have designed a form containing data and I want to print that form and the
data as it appears in the Windows Form. I am a beginner with C# and I have
read several "help"s on PrintPage, PrintDialog, etc. but none seem to address
the issue of printing directly from the form. They all talk about printing
from a text file.

Can someone tell me how to print the form and maybe direct me on which to
use PrintPage, PrintDialog....which?

Thanks
D

Nov 17 '05 #4
Some of the code in there is specific to my program. I didn't sanitize
before I sent it. Oddly enough, you will need to attach to the printing
events though.

_ActiveFormImage is a private type of bitmap that I'm using to pass the
image around between methods which are managing printing events.

Check this site out...it was made for you:
http://www.codeproject.com/csharp/ImageCapture.asp
CsharpNewcommer wrote:
Hey Gary,

Thanks for the code. As I said in my earlier reply I'm using VS2003 and I
guess I have to import that "gdi32.dll" but using your example I'm not sure
how to do that other than it requires "static extern". This is a little over
my head at this point.

Anyway I tried your code and I seem to be missing some key points. For one
I'm using the click event of a button so I don't think I need:
"ToolStripMenuItem MenuItem=(ToolStripMenuItem)sender;"
MessageBox.Show(MenuItem.Name);
Two, don't I need to call PrintActiveForm() and PrtDoc() ? And three, these
are some of the build errors I'm getting:

'TestDB.fclsPastInfo' does not contain a definition for '_ActiveFormImage'

'The name '_ActiveFormImage' does not exist in the class or namespace
'TestDB.fclsPastInfo'

The type or namespace name 'PrtDoc' could not be found (are you missing a
using directive or an assembly reference?)

The type or namespace name 'PrtDoc' could not be found (are you missing a
using directive or an assembly reference?)
D.

"Gary Holbrook" wrote:

I can give you some generic information. It depends on what version of
VS you're using. You need to "take a picture" of the form, and pass
that to the drawing object when you print. With VS 2005, there is
actually a copy function you can use. With older versions you may need
to import GDI32 and make calls to that.

This is the code I use to print a form:

protected void PrintActiveForm(object sender, EventArgs e)
{
ToolStripMenuItem MenuItem=(ToolStripMenuItem)sender;
//MessageBox.Show(MenuItem.Name);
PrintDialog PD = new PrintDialog();
PrinterSettings PS = new PrinterSettings();
PrintDocument PrtDoc = new PrintDocument();

_ActiveFormImage = SetSeg_Library.SetCapture.Form(this);

PrtDoc.PrintPage += new PrintPageEventHandler(PrtDoc_PrintPage);
PD.Document = PrtDoc;
PD.ShowDialog();
PrtDoc.Print();

}

void PrtDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(this._ActiveFormImage, 0, 0);
}

CsharpNewcommer wrote:
Hi

I have designed a form containing data and I want to print that form and the
data as it appears in the Windows Form. I am a beginner with C# and I have
read several "help"s on PrintPage, PrintDialog, etc. but none seem to address
the issue of printing directly from the form. They all talk about printing
from a text file.

Can someone tell me how to print the form and maybe direct me on which to
use PrintPage, PrintDialog....which?

Thanks
D

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jody Gelowitz | last post: by
4 posts views Thread by John Baker | last post: by
5 posts views Thread by Randy | last post: by
2 posts views Thread by sitemap | last post: by
2 posts views Thread by =?Utf-8?B?Umlja1NlYW4=?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.