Name 'Printer' is not declared VB6 to .NET
Asked Answered
C

4

5

I am upgrading VB6 to .NET after the upgrade I get the compile error:

Name 'Printer' is not declared

My code in VB6 is something like this:

THeight = Printer.TextHeight("#")

What is the correct way to declare a printer in .NET?

Note: I tried to download the printer power pack, but was unable to get it to work.

Clock answered 23/10, 2014 at 17:48 Comment(0)
C
6

Visual Basic 6.0 had an intrinsic Printer object that you could use without explicitly declaring it. In contrast, the Printer Compatibility library behaves like any other .NET Framework object; you must explicitly declare a .NET Framework Printer object before you can use it.

After you upgrade your project, you could add Printer object like this:

1) On the Project menu, click Add Reference.

2) In the Add Reference dialog box, on the .NET tab, click Microsoft.VisualBasic.PowerPacks.Printing.Printer, and then click OK.

3) In the Code Editor, add the following statement at the top of the module that contains your Visual Basic 6.0 Printer code:

Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6

4) Add the following code at the top of the procedure that contains Printer code:

Public Printer As New Printer

Clock answered 23/10, 2014 at 21:24 Comment(0)
P
4

Take a look at PrintDocument in the System.Drawing.Printing namespace.

You can also find a tutorial here, which covers your problem.

The other answers are suggesting using the Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6 namespace, but as per MSDN:

This namespace enables Visual Basic 6.0 Printer code to run without modification in an upgraded project; it is not intended for new development. For new development, use the PrintDocument component.

Prefer answered 23/10, 2014 at 20:6 Comment(0)
J
1

From MSDN:

Dim Printer As New Printer
Dim msg As String = "String to measure"
Printer.Print(Printer.TextHeight(msg) & " by " & _
    Printer.TextWidth(msg) & " twips")
Printer.EndDoc()

Full documentation here: http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.powerpacks.printing.compatibility.vb6.printer.textheight.aspx

Jones answered 23/10, 2014 at 20:28 Comment(0)
H
1

Printing in VB.NET is very different to VB6. Here's sample code to get you started. I'd recommend you consider "printing" to PDF instead, e.g. using the PDFSharp library. PDFSharp is more like the VB6 printer object, and you get a PDF of the document as an added bonus.

''' <summary>
''' Bare bones printout
''' </summary>
''' <remarks></remarks>
Public Class SimplePrintout
  'USAGE: 
  'Dim spo As New SimplePrintout
  'spo.PrintPreview()

  Public Sub Print(Optional ByVal PrinterName As String = "")

    'create the document object
    Using pdcNew As New Printing.PrintDocument

      'wire up event handlers to handle pagination
      AddHandler pdcNew.PrintPage, AddressOf PrintPage

      Using docOutput As Printing.PrintDocument = pdcNew

        If PrinterName > "" Then
          docOutput.PrinterSettings.PrinterName = PrinterName
        End If
        docOutput.Print()
      End Using
    End Using
  End Sub
  ''' <summary>
  ''' Preview the Report on screen
  ''' </summary>
  ''' <remarks></remarks>
  Public Sub PrintPreview(Optional ByVal Owner As Form = Nothing)

    'create the document object
    Using pdcNew As New Printing.PrintDocument

      'wire up event handlers to handle pagination
      AddHandler pdcNew.PrintPage, AddressOf PrintPage

      Using ppvPreview As New PrintPreviewDialog
        ppvPreview.Document = pdcNew
        ppvPreview.FindForm.WindowState = FormWindowState.Maximized
        If IsNothing(Owner) Then
          ppvPreview.ShowDialog()
        Else
          ppvPreview.ShowDialog(Owner)
        End If
      End Using
    End Using
  End Sub
  Sub PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
    Dim g As Graphics = e.Graphics 'shortcut
    Dim x As Single = e.MarginBounds.Left '"Cursor" location
    Dim y As Single = e.MarginBounds.Top  '"Cursor" location
    'g.DrawRectangle(Pens.Black, e.MarginBounds) '>>DEBUG: use this line to check margins        

    Dim fnt1 As New Font(System.Drawing.FontFamily.GenericSansSerif, 12, FontStyle.Regular, GraphicsUnit.Point)
    g.DrawString("Simple printout line 1" & vbCrLf & " after CRLF", fnt1, Brushes.Black, x, y)
    y += fnt1.GetHeight(g)
    y += fnt1.GetHeight(g)
    g.DrawString("Simple printout line 2", fnt1, Brushes.Black, x, y)
    y += fnt1.GetHeight(g)
    g.DrawString("Simple printout line 3", fnt1, Brushes.Black, x, y)
    y += fnt1.GetHeight(g)

    e.HasMorePages = False
  End Sub
End Class
Hemichordate answered 23/10, 2014 at 23:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.