473,419 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 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 2794
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been...
4
by: John Baker | last post by:
Hi: I have a form, which contains a sub form (Employee main record and payment record). It all shows up fine in a screen view, but when I come to print it I find that I cannot get the sub form...
5
by: Randy | last post by:
In my MDI parent, I can do the following: Form childForm = (Form)this.ActiveMdiChild; I'd like to send the childForm in it's current state to the printer. Current state being whatever text is...
5
by: VMI | last post by:
I have a BMP image (the form is also in PDF) that contains a scanned copy of a paper form that we need to fill out. Is it possible to use this image in my application so that the application can...
0
by: Tat | last post by:
Hi, I am printing the form. The code is 100% accurate. It is the same as in MSDN, but in C#. ...
0
by: Ann | last post by:
I have a C# application that uses PrintDocument and PrintPage to draw reports. Now I need to integrate a tax form printing from a legacy VC++ DLL into the C# application. One of the important...
2
by: sitemap | last post by:
Hello guys, I have a big problem with printing of windows.Forms :( I have application with "print" button. I am trying to use "e.Graphics.CopyFromScreen"... but in final result, over the...
2
by: =?Utf-8?B?Umlja1NlYW4=?= | last post by:
I have a long form within a panel which exceeds the maximum height of 876. I use the following code to print the form but it prints only the portion of the form that is displayed on the screen (max...
0
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.