PrinterName プロパティが有効なプリンタに指定されているかどうかを示す値を取得します。
Public ReadOnly Property IsValid As Boolean
[C#]
public bool IsValid {get;}
[C++]
public: __property bool get_IsValid();
[JScript]
public function get IsValid() : Boolean;
プロパティ値
PrinterName プロパティが有効なプリンタの場合は true 。それ以外の場合は false 。
解説
プロパティによっては、取得または設定するときに有効なプリンタが指定されていないと、例外が発生します。例外が発生しないように、 PrinterName を設定した後で IsValid プロパティを使用して、プリンタが有効かどうかを安全に判断します。
使用例
[Visual Basic, C#, C++] PrinterName プロパティを設定して、印刷先プリンタを指定する例を次に示します。 IsValid が true の場合は、指定したプリンタでドキュメントが印刷されます。この例は、次の 3 つのことを前提にしています。変数 filePath に出力先のファイル パスが設定されていること、 PrintPage イベントを処理するメソッド pd_PrintPage が定義されていること、および変数 printer にプリンタ名が設定されていることです。
[Visual Basic, C#, C++] この例では、 System.Drawing 、 System.Drawing.Printing 、および System.IO の各名前空間を使用します。
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
[C#]
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);
}
}
[C++]
public:
void Printing(String* printer) {
try {
streamToPrint = new StreamReader (filePath);
try {
printFont = new System::Drawing::Font(S"Arial", 10);
PrintDocument* pd = new PrintDocument();
pd->PrintPage += new PrintPageEventHandler(this, &Form1::pd_PrintPage);
// Specify the printer to use.
pd->PrinterSettings->PrinterName = printer;
if (pd->PrinterSettings->IsValid) {
pd->Print();
}
else {
MessageBox::Show(S"Printer is invalid.");
}
}
__finally {
streamToPrint->Close();
}
}
catch(Exception* ex) {
MessageBox::Show(ex->Message);
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン
をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
PrinterSettings クラス | PrinterSettings メンバ | System.Drawing.Printing 名前空間 | PrinterName