Handling output from printer dialog box

S

StrandElectric

Morning all

Here is a snippet of code

Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6
Imports System.Drawing
'Added a reference to: Microsoft.VisualBasic.PowerPacks.Vs

Public Class Form1
....
Dim PrintDialog1 As New PrintDialog() 'just shows dialog and doesn't
use it
PrintDialog1.ShowDialog() 'more coding to do to read the ouput from
the dialog

I've looked everywhere for tips on how to access the output of this dialog.
Any suggestions
 
A

Armin Zingler

Am 28.01.2011 20:23, schrieb StrandElectric:
Morning all

Here is a snippet of code

Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6
Imports System.Drawing
'Added a reference to: Microsoft.VisualBasic.PowerPacks.Vs

Public Class Form1
....
Dim PrintDialog1 As New PrintDialog() 'just shows dialog and doesn't
use it
PrintDialog1.ShowDialog() 'more coding to do to read the ouput from
the dialog

I've looked everywhere for tips on how to access the output of this dialog.
Any suggestions

1. ShowDialog is a function. It returns OK if the user pressed ok, or Cancel if
the dialog has been cancelled:

If PrintDialog1.ShowDialog() = DialogResult.Ok Then
'Ok pressed. Print here.
End If

2. You can read the settings chosen in the dialog from the properties
of PrintDialog1.PrinterSettings. Using the PrintDocument, it would be
one line. Using the Printer object, you must assign all values individually.
See the example below.

3. Choosing the selected printer is even more complicated because you must
take the value of PrintDialog1.PrinterSettings.PrinterName and search for the
right printer in the Printers collection. I wrote the function for you
so you can just use it: (don't care about the content)

Private Shared Function GetPrinterByName(ByVal Name As String) As Printer

Return Printers.Cast(Of Printer).FirstOrDefault(Function(p) p.DeviceName = Name)

End Function


Back to #2: (the code you were asking for)

If PrintDialog1.ShowDialog = DialogResult.OK Then
Dim settings = PrintDialog1.PrinterSettings
Dim p = GetPrinterByName(settings.PrinterName)

If settings.DefaultPageSettings.Landscape Then
p.Orientation = vbPRORLandscape
Else
p.Orientation = vbPRORPortrait
End If

'Set other properties of 'p' here ........

'Print
p.Print("success")
p.EndDoc()

End If
 
S

StrandElectric

Armin Zingler said:
Am 28.01.2011 20:23, schrieb StrandElectric:

1. ShowDialog is a function. It returns OK if the user pressed ok, or
Cancel if
the dialog has been cancelled:

If PrintDialog1.ShowDialog() = DialogResult.Ok Then
'Ok pressed. Print here.
End If

2. You can read the settings chosen in the dialog from the properties
of PrintDialog1.PrinterSettings. Using the PrintDocument, it would be
one line. Using the Printer object, you must assign all values
individually.
See the example below.

3. Choosing the selected printer is even more complicated because you must
take the value of PrintDialog1.PrinterSettings.PrinterName and search for
the
right printer in the Printers collection. I wrote the function for you
so you can just use it: (don't care about the content)****I'd like to
understand it!

Private Shared Function GetPrinterByName(ByVal Name As String) As
Printer

Return Printers.Cast(Of Printer).FirstOrDefault(Function(p)
p.DeviceName = Name)

End Function


Back to #2: (the code you were asking for)

If PrintDialog1.ShowDialog = DialogResult.OK Then
Dim settings = PrintDialog1.PrinterSettings
Dim p = GetPrinterByName(settings.PrinterName)

If settings.DefaultPageSettings.Landscape Then
p.Orientation = vbPRORLandscape
Else
p.Orientation = vbPRORPortrait
End If

'Set other properties of 'p' here ........

'Print
p.Print("success")
p.EndDoc()

End If

Thank you very much Armin. Could you refer to **** above. I'd like to
understand it provided it's not too abstract.
 
A

Armin Zingler

Am 29.01.2011 20:35, schrieb StrandElectric:
Thank you very much Armin. Could you refer to **** above. I'd like to
understand it provided it's not too abstract.

It contains three topics that I wouldn't put on the schedule
in the foreseeable future:

- extension members ('Cast' is an extension member)
- generics ('Cast' is a generic function)
- lambda expressions ('Function(p) ...')

But to give a one-sentence explanation on each:
- You can extend a type by writing extension members without
using inheritance. The extension member can be used as if it
was a member of the type.
- Generics are a kind of "incomplete" members because only a
placeholder for the type is given, so the type is not specified
before it's usage.
- A lambda expression is a kind of inline-function.

At the moment, I would ignore these features completely. They've been
introduced in the later versions and do not belong to the basics, i.e.
you can (and IMO should) do well without.
 
S

StrandElectric

Armin Zingler said:
Am 29.01.2011 20:35, schrieb StrandElectric:

It contains three topics that I wouldn't put on the schedule
in the foreseeable future:

- extension members ('Cast' is an extension member)
- generics ('Cast' is a generic function)
- lambda expressions ('Function(p) ...')

But to give a one-sentence explanation on each:
- You can extend a type by writing extension members without
using inheritance. The extension member can be used as if it
was a member of the type.
- Generics are a kind of "incomplete" members because only a
placeholder for the type is given, so the type is not specified
before it's usage.
- A lambda expression is a kind of inline-function.

At the moment, I would ignore these features completely. They've been
introduced in the later versions and do not belong to the basics, i.e.
you can (and IMO should) do well without.

Makes sense!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top