Let the user select a printer and then send a printout directly to it in C#

When it starts, the program uses the following code to display the available printers in a ComboBox.

// List the available printers.
private void Form1_Load(object sender, EventArgs e)
{
foreach (string printer in PrinterSettings.InstalledPrinters)
{
cboPrinter.Items.Add(printer);
}
}

This code loops through the System.Drawing.Printing.PrinterSettings.InstalledPrinters collection, adding each printer's name to the ComboBox.

When the user selects a printer and clicks the Print button, the following code sends the printout to the selected printer.

// Print.
private void btnPrint_Click(object sender, EventArgs e)
{
// Select the printer.
pdocSmiley.PrinterSettings.PrinterName = cboPrinter.Text;

// Print.
pdocSmiley.Print();
}

This code sets the PrintDocument's printer name to the selected printer's name and then calls the PrintDocument's Print method to generate the printout.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.