In the following program, we will list the printers installed in the system in the list box, and set the default value for the default printer.
In the following program, we used two main classes to list all the printers and use the printersettings class to obtain the default printer and use the printdocument class, let's start to practice it.
Create a new windows form project and add a lable and a combox. The key is as follows. To obtain the default printer, use the following statement.
Printdocument prtdoc = new printdocument ();
String strdefaprinprinter = prtdoc. printersettings. printername; // get the default printer name
With the default printer, we can list all the printers.
The printersettings class has an installedprinters attribute. If you do not know what it is, check the msdn explanation as follows:
Printersettings. installedprinters gets the names of all printers installed on the computer.
As defined in C:
[C #]
[Serializable]
[Comvisible (false)]
Public static printersettings. stringcollection installedprinters
{Get ;}
Attribute Value
Printersettings. stringcollection, which indicates the names of all printers installed on the computer.
Exception
Exception type condition
Win32exception failed to enumerate available printers
Remarks
You can use a set of installed printer names to select the printer to be printed.
The following example fills the comboinstalledprinters combo box with an installed printer and uses the printername attribute to set the printer to be printed when you select to change it. Populateinstalledprinterscombo is called during form initialization. This example assumes that the printdocument variable named printdoc exists and a specific combo box exists.
[C #]
// Add the self-translated text in the brackets below
Private void populateinstalledprinterscombo ()
{
// Add list of installed printers found to the combo box. (add all the hosts in the system to the list box)
// The pkinstalledprinters string will be used to provide the display string. (The strings displayed in the list box are provided by pkinstalledprinters)
Foreach (string pkinstalledprinters in
Printersettings. installedprinters)
{
Comboinstalledprinters. Items. Add (pkinstalledprinters );
}
}
Private void comboinstalledprinters_selectionchanged (Object sender, system. eventargs E)
{
// Set the printer to a printer in the combo box when the selection changes. (set the selected printer when the list box is changed)
If (comboinstalledprinters. selectedindex! =-1)
{
// The combo box's text property returns the selected item's text, which is the printer name. (display the selected printer name in the list box)
Printdoc. printersettings. printername = comboinstalledprinters. text;
}
}
After reading the msdn instructions, I understand more. below is the complete code for writing exercises.
// Program description: list all printers in the system in the list box
// Program variable: printdocument prtdoc, string strdefaprinprinter
// Prepared by: silkworm (sillnet@163.net)
// Date: 2003-20
Using system;
Using system. drawing;
Using system. Drawing. printing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Namespace printerlist
{
///
/// Summary of form1.
///
Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. Label label1;
Private system. Windows. Forms. ComboBox printerlist;
///
/// Required designer variables.
///
Private system. componentmodel. Container components = NULL;
Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
Printdocument prtdoc = new printdocument ();
String strdefaprinprinter = prtdoc. printersettings. printername; // get the default printer name
Foreach (string strprinter in printersettings. installedprinters)
// List all printers in the list box,
{
Printerlist. Items. Add (strprinter );
If (strprinter = strdefaprinprinter) // set the default printer to the default value.
{
Printerlist. selectedindex = printerlist. Items. indexof (strprinter );
}
}
//
// Todo: add Any constructor code after initializecomponent calls
//
}
///
/// Clear all resources in use.
///
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region windows Form Designer generated code
///
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
///
Private void initializecomponent ()
{
This. label1 = new system. Windows. Forms. Label ();
This. printerlist = new system. Windows. Forms. ComboBox ();
This. suspendlayout ();
//
// Label1
//
This. label1.location = new system. Drawing. Point (8, 24 );
This. label1.name = "label1 ";
This. label1.size = new system. Drawing. Size (72, 16 );
This. label1.tabindex = 0;
This. label1.text = "select printer :";
//
// Printerlist
//
This. printerlist. Location = new system. Drawing. Point (88, 22 );
This. printerlist. Name = "printerlist ";
This. printerlist. size = new system. Drawing. Size (192, 21 );
This. printerlist. tabindex = 1;
This. printerlist. Text = "no printer is installed in the current system ";
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (5, 13 );
This. clientsize = new system. Drawing. Size (288, 61 );
This. Controls. addrange (new system. Windows. Forms. Control [] {
This. printerlist,
This. label1 });
This. Name = "form1 ";
This. Text = "Printer list ";
This. resumelayout (false );
}
# Endregion
///
/// Main entry point of the application.
///
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
}
}