views:

338

answers:

2

I'd like to be able to specify two different printers for two different jobs. I'm using the following class to handle printing these, but regardless of what I do, the default printer is always the one that's printed to.

Public Class Receipt : Inherits Printing.PrintDocument
Private _font As Font = New Font("Courier", 8)
Private _text As String = ""

Public Property Text() As String
    Get
        Return _text
    End Get
    Set(ByVal Value As String)
        _text = Value.Trim
    End Set
End Property

Public Sub New(ByVal str As String, ByVal settings As Printing.PrinterSettings)
    MyBase.New()
    _text = str
    Me.PrinterSettings = settings
End Sub

Protected Overrides Sub OnPrintPage(ByVal e As Printing.PrintPageEventArgs)
    Dim printHeight As Integer
    Dim printWidth As Integer
    Dim leftMargin As Integer
    Dim rightMargin As Integer


    With Me.DefaultPageSettings
        .PaperSize = New System.Drawing.Printing.PaperSize("Custom", 300, 1200)
        .Margins.Left = 25
        .Margins.Right = 25

        printHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
        printWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
        leftMargin = .Margins.Left
        rightMargin = .Margins.Top
    End With

    Dim printArea As New RectangleF(leftMargin, rightMargin, printWidth, printHeight)
    Dim format As New StringFormat(StringFormatFlags.LineLimit)

    Try
        e.Graphics.DrawString(_text, _font, Brushes.Black, printArea, format)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
End Class

If I inspect my PrinterSettings attribute immediately before the call to DrawString, the PrinterName attribute is still correctly set to the printer I specify, but it's still the default printer that kicks out the job. I'm sure I'm missing something obvious, but would certainly appreciate if someone could point out what it is. :)

Thanks

A: 

The PrinterSettings.PrinterName property is actually what you should be using.

You can get a list of installed printers using PrinterSettings.InstalledPrinters (System.Drawing.Printing namespace). Perhaps your provider printer name is slightly different from what it should be, because I can confirm this actually works.

Thorarin
+1  A: 

I just created a test app with the class code you posted and it works fine. It uses whatever printer I select. So I must conclude that wherever you are using this class you're accidentally altering the PrintSettings object after you initialize the object but before you call Print.

Or perhaps the printer name you specify isn't valid and the default is used as a backup. You can check this using PrinterSettings.IsValid after setting the PrinterName property.

Corin
I'm admittedly baffled. Before I left the office on Friday, I had set flags to verify that PrinterSettings.IsValid came back true, and indeed it did, but the problem persists. Now as I come in Monday morning, I tried it again reading your and Thorarin's confirmation that what I've done should work, and low and behold, it does. Don't know what was different on Friday, but thanks for confirming I was on the right track and preventing me from going down paths I didn't need to go down. :)
Blumer