Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » Windows Controls C# » PrintDialog in C#

PrintDialog in C#

A PrintDialog control is used to open the Windows Print Dialog and let user select the printer, set printer and paper properties and print a file.

Author Rank :
Page Views : 22180
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 



A PrintDialog control is used to open the Windows Print Dialog and let user select the printer, set printer and paper properties and print a file. A typical Open File Dialog looks like Figure 1 where you select a printer from available printers, set printer properties, set print range, number of pages and copies and so on. Clicking on OK button sends the document to the printer.

PrintDlgImg1.jpg
Figure 1

Creating a PrintDialog

We can create a PrintDialog at design-time as well as at run-time.

Design-time

To create a PrintDialog control at design-time, you simply drag and drop a PrintDialog control from Toolbox to a Form in Visual Studio. After you drag and drop a PrintDialog on a Form, the PrintDialog looks like Figure 2.

PrintDlgImg2.jpg
Figure 2

Run-time

Creating a PrintDialog control at run-time is simple. First step is to create an instance of PrintDialog class and then call the ShowDialog method. The following code snippet creates a PrintDialog control.

PrintDialog PrintDialog1 = new PrintDialog();

PrintDialog1.ShowDialog();


Printing Documents


PrintDocument object represents a document to be printed. Once a PrintDocument is created, we can set the Document property of PrintDialog as this document. After that we can also set other properties.  The following code snippet creates a PrintDialog and sends some text to a printer.

private void PrintButton_Click(object sender, EventArgs e)

{

    PrintDialog printDlg = new PrintDialog();

    PrintDocument printDoc = new PrintDocument();

    printDoc.DocumentName = "Print Document";

    printDlg.Document = printDoc;

    printDlg.AllowSelection = true;

    printDlg.AllowSomePages = true;

    //Call ShowDialog

    if (printDlg.ShowDialog() == DialogResult.OK)

        printDoc.Print();

 

}

 

More Readings

I do not want to duplicate the work but here are various articles on Printing.

 

Summary

A PrintDialog control allows users to launch Windows Open File Dialog and let them select files. In this article, we discussed how to use a Windows Open File Dialog and set its properties in a Windows Forms application.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Mindcracker MVP Summit 2012
Become a Sponsor
 Comments
Re: Great post by Mahesh On November 7, 2010
You're welcome David.
Reply | Email | Modify 
con't take print by shashi On December 15, 2010
Hi this is deepak i am trying to print the contant of richText Box ,i have used many lables in richbox all lables takes data from database when print button to print the result but i am getting only blank page no any data why here is my code plz tell me whats there problem on my ID "Deepak.pandit2@gmail.com"reply soon plzzzzz. [ protected void printToolStripMenuItem_Click(object sender, System.EventArgs e) { try { string strText = this.richTextBox1.Text; myReader = new StringReader(strText); PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog(); printPreviewDialog1.Document = this.ThePrintDocument; printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D; printPreviewDialog1.ShowDialog(); } catch (Exception exp) { System.Console.WriteLine(exp.Message.ToString()); } } protected void ThePrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev) { float linesPerPage = 0; float yPosition = 0; int count = 0; float leftMargin = ev.MarginBounds.Left; float topMargin = ev.MarginBounds.Top; string line = null; Font printFont = this.richTextBox1.Font; SolidBrush myBrush = new SolidBrush(Color.Black); // Work out the number of lines per page, using the MarginBounds. linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics); // Iterate over the string using the StringReader, printing each line. while (count < linesPerPage && ((line = myReader.ReadLine()) != null)) { // calculate the next line position based on the height of the font according to the printing device yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics)); // draw the next line in the rich edit control ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat()); count++; } // If there are more lines, print another page. if (line != null) ev.HasMorePages = true; else ev.HasMorePages = false; myBrush.Dispose(); } private void fileToolStripMenuItem_Click(object sender, EventArgs e) { } protected void exitToolStripMenuItem_Click(object sender, EventArgs e) { printDialog1.Document = ThePrintDocument; string strText = this.richTextBox1.Text; myReader = new StringReader(strText); if (printDialog1.ShowDialog() == DialogResult.OK) { try { this.ThePrintDocument.Print(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { MessageBox.Show("Printer nt inistall"); } } } ]
Reply | Email | Modify 
namespace by Gabor On February 28, 2011
Nb, this PrintDialog is from the System.Windows.Forms namespace, not System.Windows.Controls. Thanks for the article!
Reply | Email | Modify 
printDlg.AllowSomePages = true; is not working by manjula On March 15, 2011
private void PrintButton_Click(object sender, EventArgs e) { PrintDialog printDlg = new PrintDialog(); PrintDocument printDoc = new PrintDocument(); printDoc.DocumentName = "Print Document"; printDlg.Document = printDoc; printDlg.AllowSelection = true; printDlg.AllowSomePages = true; //Call ShowDialog if (printDlg.ShowDialog() == DialogResult.OK) printDoc.Print(); } above code I tryed to set max copy count in to print dialog & also AllowSelection = true ; is not working
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.