--- title: "How to: Choose the Printers Attached to a User's Computer" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" - "cpp" helpviewer_keywords: - "printing [Windows Forms], choosing printers" - "printers [Windows Forms], choosing" ms.assetid: 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 component. Through the component, the of the 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 class is then instantiated. ### To choose a printer and then print a file 1. Select the printer to be used using the component. In the following code example, there are two events being handled. In the first, a control's event, the class is instantiated and the printer selected by the user is captured in the property. In the second event, the event of the component, a sample document is printed to the printer specified. ```vb 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 Sub ``` ```csharp private 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)); } ``` ```cpp 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. ```csharp this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler (this.printDocument1_PrintPage); this.button1.Click += new System.EventHandler(this.button1_Click); ``` ```cpp this->printDocument1->PrintPage += gcnew System::Drawing::Printing::PrintPageEventHandler (this, &Form1::printDocument1_PrintPage); this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); ``` ## See also - [Windows Forms Print Support](windows-forms-print-support.md)