PrinterSettings.IsValid Свойство

Определение

Получает значение, определяющее, назначает ли свойство PrinterName действительный принтер.

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

Значение свойства

true, если свойство PrinterName назначает действительный принтер, в противном случае — false.

Примеры

В следующем примере кода задается целевой принтер, задав PrinterName свойство , а если IsValid имеет значение true, документ печатается на указанном принтере. В этом примере есть три предварительных условия:

  • Переменной с именем filePath задан путь к файлу для печати.

  • Определен метод с именем pd_PrintPage, который обрабатывает PrintPage событие.

  • Для переменной с именем printer задано имя принтера.

В этом примере используйте System.Drawingпространства имен , System.Drawing.Printingи System.IO .

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

Комментарии

При получении или установке некоторых свойств требуется допустимый принтер или возникает исключение. Чтобы избежать исключений IsValid , используйте свойство после задания PrinterName , чтобы безопасно определить, является ли принтер допустимым.

Применяется к

См. также раздел