Problem : Invalid Printer Exception – when no printers are installed

Problem : Invalid Printer Exception – when no printers are installed

Hi Guys,

I’ve got a vb.net program which takes a load of fields from my form and does a g.DrawString style thing onto the page.
If the system has a printer installed then this works great, if there is no printer installed it throws up an error :
——
System.Drawing.Printing.InvalidPrinterException: No printers installed.
——
Is there a way to test to see if any printers are installed before doing the ‘PrintPreviewDialog1.ShowDialog()’
or perhaps a way of catching the error into producing a nice message box, telling the user that they don’t have a printer therefore they cannot print DOH! and then return to the program when they click ok.

Any help would be great as I have to present this piece of software at a meeting tomorrow(wed) afternoon!
Agh!!!!!!!

Many Thanks


Solution : Invalid Printer Exception – when no printers are installed

do u have a

try

catch

around that section of code.

to check for printers, u could use this code:

create a new project and copy paste this code for a sample
Public Class Form1
Inherits System.Windows.Forms.Form

#Region ” Windows Form Designer generated code ”

Public Sub New()
MyBase.New()

‘This call is required by the Windows Form Designer.
InitializeComponent()

‘Add any initialization after the InitializeComponent() call

End Sub

‘Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

‘Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

‘NOTE: The following procedure is required by the Windows Form Designer
‘It can be modified using the Windows Form Designer.
‘Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
<system.diagnostics.debuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.SuspendLayout()

‘Button1

Me.Button1.Location = New System.Drawing.Point(96, 88)
Me.Button1.Name = “Button1”
Me.Button1.Size = New System.Drawing.Size(56, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = “Button1”

‘ListBox1

Me.ListBox1.Location = New System.Drawing.Point(88, 152)
Me.ListBox1.Name = “ListBox1”
Me.ListBox1.Size = New System.Drawing.Size(120, 95)
Me.ListBox1.TabIndex = 1

‘Form1

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Button1)
Me.Name = “Form1”
Me.Text = “Form1”
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim a As New Printing.PageSettings
Dim b As System.Drawing.Printing.PrinterSettings.StringCollection = a.PrinterSettings.InstalledPrinters
Dim X As Integer
For X = X To b.Count – 1
listbox1.Items.Add(b.Item(X))
Next
End Sub
End Class</system.diagnostics.debugg