* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
4.3 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||
|---|---|---|---|---|---|---|---|---|---|
| How to: Choose the Printers Attached to a User's Computer | 03/30/2017 |
|
|
63c1172b-2931-4ac0-953f-37f629494bbf |
How to: Choose the Printers Attached to a User's Computer in Windows Forms
Often, users want to choose a printer other than the default printer to print to. You can enable users to choose a printer from among those currently installed by using the xref:System.Windows.Forms.PrintDialog component. Through the xref:System.Windows.Forms.PrintDialog component, the xref:System.Windows.Forms.DialogResult of the xref:System.Windows.Forms.PrintDialog component is captured and used to select the printer.
In the following procedure, a text file is selected to be printed to the default printer. The xref:System.Windows.Forms.PrintDialog class is then instantiated.
To choose a printer and then print a file
-
Select the printer to be used using the xref:System.Windows.Forms.PrintDialog component.
In the following code example, there are two events being handled. In the first, a xref:System.Windows.Forms.Button control's xref:System.Windows.Forms.Control.Click event, the xref:System.Windows.Forms.PrintDialog class is instantiated and the printer selected by the user is captured in the xref:System.Windows.Forms.DialogResult property.
In the second event, the xref:System.Drawing.Printing.PrintDocument.PrintPage event of the xref:System.Drawing.Printing.PrintDocument component, a sample document is printed to the printer specified.
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim PrintDialog1 As New PrintDialog() PrintDialog1.Document = PrintDocument1 Dim result As DialogResult = PrintDialog1.ShowDialog() If (result = DialogResult.OK) Then PrintDocument1.Print() End If End Sub Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage e.Graphics.FillRectangle(Brushes.Red, New Rectangle(500, 500, 500, 500)) End Subprivate void button1_Click(object sender, System.EventArgs e) { PrintDialog printDialog1 = new PrintDialog(); printDialog1.Document = printDocument1; DialogResult result = printDialog1.ShowDialog(); if (result == DialogResult.OK) { printDocument1.Print(); } } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.FillRectangle(Brushes.Red, new Rectangle(500, 500, 500, 500)); }private: void button1_Click(System::Object ^ sender, System::EventArgs ^ e) { PrintDialog ^ printDialog1 = gcnew PrintDialog(); printDialog1->Document = printDocument1; System::Windows::Forms::DialogResult result = printDialog1->ShowDialog(); if (result == DialogResult::OK) { printDocument1->Print(); } } private: void printDocument1_PrintPage(System::Object ^ sender, System::Drawing::Printing::PrintPageEventArgs ^ e) { e->Graphics->FillRectangle(Brushes::Red, Rectangle(500, 500, 500, 500)); }(Visual C# and Visual C++) Place the following code in the form's constructor to register the event handler.
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler (this.printDocument1_PrintPage); this.button1.Click += new System.EventHandler(this.button1_Click);this->printDocument1->PrintPage += gcnew System::Drawing::Printing::PrintPageEventHandler (this, &Form1::printDocument1_PrintPage); this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);