By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,687 Members | 2,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,687 IT Pros & Developers. It's quick & easy.

Printing an Form

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.