Installed Printers in 2005 beta 2

I am trying to get a list of installed printers. I added a form and a printdialog1 and the following code:

Dim iAsInteger

Dim pkInstalledPrintersAsString

Dim printdocAsString

For i = 0To PrintDialog1.InstalledPrinters.Count - 1

pkInstalledPrinters = PrinterSettings.InstalledPrinters.Item(i)

comboinstalledprinters.Items.Add(pkInstalledPrinters)

Next

ForEach pkInstalledPrintersIn PrinterSettings.InstalledPrinters

comboinstalledprinters.Items.Add(pkInstalledPrinters)

Next

1. printdialog1.installedprinters returns a message not being a member of printdialog1.
2. PrinterSettings shows as not being declared.

This code if from MSDN, probably for .net2003

[1705 byte] By [jmorgan] at [2008-2-17]
# 1
Hi,

Try this,
Dim i As Integer
Dim pkInstalledPrinters As String

For i = 0 to PrintDialog1.PrinterSettings.InstalledPrinters.Count - 1
pkInstalledPrinters = PrintDialog1.PrinterSettings.InstalledPrinters.Item(i)
comboinstalledprinters.Items.Add(pkInstalledPrinters)
Next

your first problem in your code is that InstalledPrinters object is not a member of printDialog1 it is really a member of PrintDialog1.PrinterSettings Object...

Your second problem is that you directly referenced PrinterSettings object without first referencing the printDialog1 Object.

If you want to create a variable to hold the printersettings object:

Dim ps As System.Drawing.Printing.PrinterSettings
ps = PrintDialog1.PrinterSettings

And just replace all the PrintDialog1.PrinterSettings instances in your code with ps.
cheers,
Paul June A. Domag

PaulDomag at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
Thanks

I changed the code to below

Dim ps As System.Drawing.Printing.PrinterSettings

ps = PrintDialog1.PrinterSettings

Dim i As Integer

Dim pkInstalledPrinters As String

For i = 0 To ps.PrinterSettings.InstalledPrinters.Count - 1

pkInstalledPrinters = ps.InstalledPrinters.Item(i)

comboinstalledprinters.Items.Add(pkInstalledPrinters)

Next

But I get a message:
- on the ps.PrinterSettings.InstalledPrinters.count -1 line of

PrinterSettings is not a member of 'System.drawing.printing.printersetting

jmorgan at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

SmileSmileThis works, I don't know why the ps variable doesn't work though?

Dim pkInstalledPrinters As String

For i = 0 To System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count - 1

pkInstalledPrinters = System.Drawing.Printing.PrinterSettings.InstalledPrinters.Item(i)

comboinstalledprinters.Items.Add(pkInstalledPrinters)

Next

jmorgan at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
Hi,

Got puzzled with that for a moment. Big Smile
you should do it this way...

Dim ps As System.Drawing.Printing.PrinterSettings

ps = PrintDialog1.PrinterSettings

Dim i As Integer

Dim pkInstalledPrinters As String

'' Edit Here-
''For
i = 0 To ps.PrinterSettings.InstalledPrinters.Count - 1
'' Change to
For i = 0 To ps.InstalledPrinters.Count - 1

pkInstalledPrinters = ps.InstalledPrinters.Item(i)

comboinstalledprinters.Items.Add(pkInstalledPrinters)

Next
But you could always use the System.Drawing.Printing.PrinterSettings.InstalledPrinters object directly for it is static and contains the list of installed printers upon runtime...
cheers,
Paul June A. Domag

PaulDomag at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic General...