Düzenle

Aracılığıyla paylaş


PrinterSettings.IsValid Property

Definition

Gets a value indicating whether the PrinterName property designates a valid printer.

public:
 property bool IsValid { bool get(); };
public bool IsValid { get; }
member this.IsValid : bool
Public ReadOnly Property IsValid As Boolean

Property Value

true if the PrinterName property designates a valid printer; otherwise, false.

Examples

The following code example specifies the target printer by setting the PrinterName property, and if the IsValid is true, prints the document on the specified printer. The example has three prerequisites:

  • A variable named filePath has been set to the path of the file to print.

  • A method named pd_PrintPage, which handles the PrintPage event, has been defined.

  • A variable named printer has been set to the printer's name.

Use the System.Drawing, System.Drawing.Printing, and System.IO namespaces for this example.

public:
   void Printing( String^ printer )
   {
      try
      {
         streamToPrint = gcnew StreamReader( filePath );
         try
         {
            printFont = gcnew System::Drawing::Font( "Arial",10 );
            PrintDocument^ pd = gcnew PrintDocument;
            pd->PrintPage += gcnew PrintPageEventHandler(
               this, &Form1::pd_PrintPage );
            // Specify the printer to use.
            pd->PrinterSettings->PrinterName = printer;
            if ( pd->PrinterSettings->IsValid )
            {
               pd->Print();
            }
            else
            {
               MessageBox::Show( "Printer is invalid." );
            }
         }
         finally
         {
            streamToPrint->Close();
         }
      }
      catch ( Exception^ ex ) 
      {
         MessageBox::Show( ex->Message );
      }
   }
public void Printing(string printer) {
  try {
    streamToPrint = new StreamReader (filePath);
    try {
      printFont = new Font("Arial", 10);
      PrintDocument pd = new PrintDocument(); 
      pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
      // Specify the printer to use.
      pd.PrinterSettings.PrinterName = printer;

      if (pd.PrinterSettings.IsValid) {
         pd.Print();
      } 
      else {	
         MessageBox.Show("Printer is invalid.");
      }
    } 
    finally {
      streamToPrint.Close();
    }
  } 
  catch(Exception ex) {
    MessageBox.Show(ex.Message);
  }
}
Public Sub Printing(printer As String)
    Try
        streamToPrint = New StreamReader(filePath)
        Try
            printFont = New Font("Arial", 10)
            Dim pd As New PrintDocument()
            AddHandler pd.PrintPage, AddressOf pd_PrintPage
            ' Specify the printer to use.
            pd.PrinterSettings.PrinterName = printer

            If pd.PrinterSettings.IsValid then
               pd.Print()
            Else
               MessageBox.Show("Printer is invalid.")
            End If
        Finally
            streamToPrint.Close()
        End Try
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Remarks

When you get or set some properties, a valid printer is required or else an exception is raised. To avoid exceptions, use the IsValid property after setting the PrinterName to safely determine if the printer is valid.

Applies to

See also