Export (0) Print
Expand All

Choosing the Printers Attached to a User's Machine in Windows Forms

Visual Studio .NET 2003

Often, users want to choose a printer other than the default printer to print to. Choosing a printer from among those currently installed is done using the PrintDialog component. The DialogResult of the PrintDialog component is then captured and used to select the printer. For more information on capturing and using the DialogResult property of a dialog box, see Closing Dialog Boxes and Retaining User Input.

In the following example, 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 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 PrintDocument component's PrintPage event, a sample document is printed to the printer specified.

    Note   For more information about working with event handlers, see Creating Event Handlers on the Windows Forms Designer.
    ' Visual Basic
    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
    
    // C#
    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));
    }
    
    // C++
    private:
       System::Void button1_Click(System::Object *  sender,
          System::EventArgs *  e)
       {
          PrintDialog * printDialog1 = new PrintDialog();
          printDialog1->Document = printDocument1;
          System::Windows::Forms::DialogResult result = 
             printDialog1->ShowDialog();
          if (result == DialogResult::OK)
          {
             printDocument1->Print();
          }
       }
    private:
       System::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++ Note   Be sure that the necessary code to enable the event handlers is present. In this case, they would be similar to the following:
    // C#
    this.printDocument1.PrintPage += new
    System.Drawing.Printing.PrintPageEventHandler
    (this.printDocument1_PrintPage);
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // C++
    this->printDocument1->PrintPage += new
    System::Drawing::Printing::PrintPageEventHandler
    (this, printDocument1_PrintPage);
    this->button1->Click += new
    System::EventHandler(this, button1_Click);

See Also

Windows Forms Print Support | Windows Forms

Show:
© 2014 Microsoft