PrinterSettings.IsValid Propriedade

Definição

Recebe um valor que indica se a PrinterName propriedade designa uma impressora válida.

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

Valor de Propriedade

true se a PrinterName propriedade designar uma impressora válida; caso contrário, false.

Exemplos

O seguinte exemplo de código especifica a impressora alvo definindo a PrinterName propriedade e, se for IsValidtrue, imprime o documento na impressora especificada. O exemplo tem três pré-requisitos:

  • Uma variável nomeada filePath foi definida para o caminho do ficheiro a imprimir.

  • Foi definido um método chamado pd_PrintPage, que gere o PrintPage evento.

  • Uma variável nomeada printer foi definida para o nome da impressora.

Use os System.Drawingnamespaces , System.Drawing.Printing, e System.IO para este exemplo.

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

Observações

Quando obténs ou defines algumas propriedades, é necessária uma impressora válida ou então surge uma exceção. Para evitar exceções, use a IsValid propriedade após definir o PrinterName para determinar com segurança se a impressora é válida.

Aplica-se a

Ver também