PrinterSettings.IsValid Propriedade
Definição
Obtém um valor que indica se a propriedade PrinterName designa uma impressora válida.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
Valor da propriedade
true se a propriedade PrinterName designar uma impressora válida; caso contrário, false.true if the PrinterName property designates a valid printer; otherwise, false.
Exemplos
O exemplo de código a seguir especifica a impressora de destino definindo a PrinterName propriedade e, se IsValid for true , imprime o documento na impressora especificada.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. O exemplo tem três pré-requisitos:The example has three prerequisites:
Uma variável chamada
filePathfoi definida como o caminho do arquivo a ser impresso.A variable namedfilePathhas been set to the path of the file to print.Um método chamado
pd_PrintPage, que manipula o PrintPage evento, foi definido.A method namedpd_PrintPage, which handles the PrintPage event, has been defined.Uma variável chamada
printerfoi definida como o nome da impressora.A variable namedprinterhas been set to the printer's name.
Use os System.Drawing System.Drawing.Printing namespaces,, e System.IO para este exemplo.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
Comentários
Quando você obtém ou define algumas propriedades, uma impressora válida é necessária ou, caso contrário, uma exceção é gerada.When you get or set some properties, a valid printer is required or else an exception is raised. Para evitar exceções, use a IsValid Propriedade depois de definir o PrinterName para determinar com segurança se a impressora é válida.To avoid exceptions, use the IsValid property after setting the PrinterName to safely determine if the printer is valid.