Partager via


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 PrintDialog component. Through the PrintDialog component, the DialogResult of the 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 PrintDialog class is then instantiated.

To choose a printer and then print a file

  • Select the printer to be used using the PrintDialog component.

    In the following code example, there are two events being handled. In the first, a Button control's Click event, the PrintDialog class is instantiated and the printer selected by the user is captured in the DialogResult property.

    In the second event, the PrintPage event of the 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 Sub
    
    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));
    }
    
    private void button1_Click(Object sender, System.EventArgs e)
    {
       PrintDialog printDialog1 = new PrintDialog();
       printDialog1.set_Document(printDocument1);
       DialogResult result = printDialog1.ShowDialog();
       if (result == DialogResult.OK)
       {
          printDocument1.Print();
       }
    }
    
    private void printDocument1_PrintPage(Object sender,
    System.Drawing.Printing.PrintPageEventArgs e)
    {
       e.get_Graphics().FillRectangle(Brushes.get_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#, Visual J#, 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.add_PrintPage(new
    System.Drawing.Printing.PrintPageEventHandler
    (this.printDocument1_PrintPage));
    this.button1.add_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);
    

See Also

Other Resources

Windows Forms Print Support