Link to home
Start Free TrialLog in
Avatar of Barkis_
Barkis_Flag for Sweden

asked on

To Print a form in .NET ....

I have just recently found an interest in making it possible for the user to be able to print a form (As it appears on screen)
I have been able to produce the printer dialog and all.  But what do I need to do to actually send it to the selected printer?
This is the code I use to show the print dialog:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim PrintDialog1 As New PrintDialog()
        Dim prnsettings As New Printing.PrinterSettings()

        prnsettings.PrinterName = "fineprint"  'example printer
        prnsettings.PrintRange = Drawing.Printing.PrintRange.AllPages

        PrintDialog1.PrinterSettings = prnsettings
        PrintDialog1.ShowDialog()

    End Sub

What am I missing?

Cheers
Barkis_
Avatar of DCD
DCD

Dim WithEvents PD As System.Drawing.Printing.PrintDocument
Dim bmp as Bitmap

Private Function GetForm() as Bitmap
     Dim rect as Rectangle
     Dim gDest as Graphics

     Rect = New Rectangle(Me.Left, Me.Top, Me.Width, Me.Height)

     Dim screenBMP As New Bitmap(Rect.Right, Rect.Bottom)
           gDest = gDest.FromImage(screenBMP)

End Function

Private Sub PrintBMP()
        PD = New Printing.PrintDocument
        Dim PGSETUP As New PageSetupDialog
        PGSETUP.PageSettings = PD.DefaultPageSettings
        If PGSETUP.ShowDialog = DialogResult.OK Then
            Dim PP As New PrintPreviewDialog
            PP.Document = PD
            PP.ShowDialog()
        End If
    End Sub


    Private Sub PD_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PD.PrintPage
        Dim img As Image = bmp
        Dim X, Y As Integer
        X = Math.Max(0, (e.PageSettings.Bounds.Width - img.Width) / 2)
        Y = Math.Max(0, (e.PageSettings.Bounds.Height - img.Height) / 2)
        e.Graphics.DrawImage(img, X, Y)
    End Sub

The call to run this would be:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      bmp =  GetForm
      PrintBMP()
End Sub
Public Class DemoForm
    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 Label1 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents ButtonPrintMe As System.Windows.Forms.Button
    Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateTimePicker
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
    Friend WithEvents Label5Hide As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents Label8 As System.Windows.Forms.Label
    Friend WithEvents Label9 As System.Windows.Forms.Label
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents CheckBoxTextBoxBoxed As System.Windows.Forms.CheckBox
    Friend WithEvents CheckBoxTabControlBoxed As System.Windows.Forms.CheckBox
    Friend WithEvents CheckBoxPrintPreview As System.Windows.Forms.CheckBox
    Friend WithEvents CheckBoxLabelInBold As System.Windows.Forms.CheckBox
    Friend WithEvents RadioButtonPortrait As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButtonLandscape As System.Windows.Forms.RadioButton
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(DemoForm))
        Me.Label1 = New System.Windows.Forms.Label
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TabControl1 = New System.Windows.Forms.TabControl
        Me.TabPage1 = New System.Windows.Forms.TabPage
        Me.Label7 = New System.Windows.Forms.Label
        Me.ComboBox1 = New System.Windows.Forms.ComboBox
        Me.Label5 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.TabPage2 = New System.Windows.Forms.TabPage
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.TextBox3 = New System.Windows.Forms.TextBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.ButtonPrintMe = New System.Windows.Forms.Button
        Me.DateTimePicker1 = New System.Windows.Forms.DateTimePicker
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.Label6 = New System.Windows.Forms.Label
        Me.Label5Hide = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.TextBox4 = New System.Windows.Forms.TextBox
        Me.Label8 = New System.Windows.Forms.Label
        Me.Label9 = New System.Windows.Forms.Label
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.RadioButtonLandscape = New System.Windows.Forms.RadioButton
        Me.RadioButtonPortrait = New System.Windows.Forms.RadioButton
        Me.CheckBoxPrintPreview = New System.Windows.Forms.CheckBox
        Me.CheckBoxLabelInBold = New System.Windows.Forms.CheckBox
        Me.CheckBoxTabControlBoxed = New System.Windows.Forms.CheckBox
        Me.CheckBoxTextBoxBoxed = New System.Windows.Forms.CheckBox
        Me.TabControl1.SuspendLayout()
        Me.TabPage1.SuspendLayout()
        Me.TabPage2.SuspendLayout()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.Panel1.SuspendLayout()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Name :"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(152, 16)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(240, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = "TextBox1"
        '
        'TabControl1
        '
        Me.TabControl1.Controls.Add(Me.TabPage1)
        Me.TabControl1.Controls.Add(Me.TabPage2)
        Me.TabControl1.ItemSize = New System.Drawing.Size(62, 30)
        Me.TabControl1.Location = New System.Drawing.Point(16, 48)
        Me.TabControl1.Name = "TabControl1"
        Me.TabControl1.SelectedIndex = 0
        Me.TabControl1.Size = New System.Drawing.Size(400, 144)
        Me.TabControl1.TabIndex = 2
        '
        'TabPage1
        '
        Me.TabPage1.Controls.Add(Me.Label7)
        Me.TabPage1.Controls.Add(Me.ComboBox1)
        Me.TabPage1.Controls.Add(Me.Label5)
        Me.TabPage1.Controls.Add(Me.Button1)
        Me.TabPage1.Controls.Add(Me.TextBox2)
        Me.TabPage1.Controls.Add(Me.Label2)
        Me.TabPage1.Location = New System.Drawing.Point(4, 34)
        Me.TabPage1.Name = "TabPage1"
        Me.TabPage1.Size = New System.Drawing.Size(392, 106)
        Me.TabPage1.TabIndex = 0
        Me.TabPage1.Text = "TabPage1"
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(8, 40)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(112, 23)
        Me.Label7.TabIndex = 7
        Me.Label7.Text = "Item:"
        Me.Label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'ComboBox1
        '
        Me.ComboBox1.Location = New System.Drawing.Point(128, 40)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(152, 21)
        Me.ComboBox1.TabIndex = 6
        Me.ComboBox1.Text = "ComboBox1"
        '
        'Label5
        '
        Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label5.Location = New System.Drawing.Point(16, 72)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(360, 23)
        Me.Label5.TabIndex = 5
        Me.Label5.Text = "Label5 - Centered"
        Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(296, 40)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 4
        Me.Button1.Text = "Button1"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(128, 8)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(248, 20)
        Me.TextBox2.TabIndex = 3
        Me.TextBox2.Text = "TextBox2 - RightAlign"
        Me.TextBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 8)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(112, 23)
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "Name 1 Right:"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'TabPage2
        '
        Me.TabPage2.Controls.Add(Me.DataGrid1)
        Me.TabPage2.Controls.Add(Me.TextBox3)
        Me.TabPage2.Controls.Add(Me.Label3)
        Me.TabPage2.Controls.Add(Me.PictureBox1)
        Me.TabPage2.Location = New System.Drawing.Point(4, 34)
        Me.TabPage2.Name = "TabPage2"
        Me.TabPage2.Size = New System.Drawing.Size(392, 106)
        Me.TabPage2.TabIndex = 1
        Me.TabPage2.Text = "TabPage2"
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(8, 40)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(256, 64)
        Me.DataGrid1.TabIndex = 4
        '
        'TextBox3
        '
        Me.TextBox3.Location = New System.Drawing.Point(132, 8)
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.Size = New System.Drawing.Size(244, 20)
        Me.TextBox3.TabIndex = 3
        Me.TextBox3.Text = "TextBox3 - Center"
        Me.TextBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(12, 8)
        Me.Label3.Name = "Label3"
        Me.Label3.TabIndex = 2
        Me.Label3.Text = "Name 2 :"
        '
        'PictureBox1
        '
        Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
        Me.PictureBox1.Location = New System.Drawing.Point(280, 40)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(96, 64)
        Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
        Me.PictureBox1.TabIndex = 12
        Me.PictureBox1.TabStop = False
        '
        'ButtonPrintMe
        '
        Me.ButtonPrintMe.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.ButtonPrintMe.ForeColor = System.Drawing.SystemColors.Highlight
        Me.ButtonPrintMe.Location = New System.Drawing.Point(16, 24)
        Me.ButtonPrintMe.Name = "ButtonPrintMe"
        Me.ButtonPrintMe.Size = New System.Drawing.Size(56, 48)
        Me.ButtonPrintMe.TabIndex = 4
        Me.ButtonPrintMe.Text = "Print me"
        '
        'DateTimePicker1
        '
        Me.DateTimePicker1.Location = New System.Drawing.Point(56, 200)
        Me.DateTimePicker1.Name = "DateTimePicker1"
        Me.DateTimePicker1.Size = New System.Drawing.Size(160, 20)
        Me.DateTimePicker1.TabIndex = 7
        '
        'Panel1
        '
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Panel1.Controls.Add(Me.Label6)
        Me.Panel1.Controls.Add(Me.Label5Hide)
        Me.Panel1.Controls.Add(Me.Label4)
        Me.Panel1.Enabled = False
        Me.Panel1.Location = New System.Drawing.Point(224, 200)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(192, 96)
        Me.Panel1.TabIndex = 8
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(24, 56)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(122, 32)
        Me.Label6.TabIndex = 2
        Me.Label6.Text = "Label4 with more than one line of text"
        '
        'Label5Hide
        '
        Me.Label5Hide.Location = New System.Drawing.Point(24, 32)
        Me.Label5Hide.Name = "Label5Hide"
        Me.Label5Hide.TabIndex = 1
        Me.Label5Hide.Text = "Label5Hidden"
        Me.Label5Hide.Visible = False
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(24, 8)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(152, 23)
        Me.Label4.TabIndex = 0
        Me.Label4.Text = "Label4 in disabled panel"
        '
        'TextBox4
        '
        Me.TextBox4.Location = New System.Drawing.Point(56, 224)
        Me.TextBox4.Multiline = True
        Me.TextBox4.Name = "TextBox4"
        Me.TextBox4.Size = New System.Drawing.Size(160, 72)
        Me.TextBox4.TabIndex = 9
        Me.TextBox4.Text = "TextBox4 multiline with more than one line"
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(16, 200)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(112, 23)
        Me.Label8.TabIndex = 10
        Me.Label8.Text = "Date:"
        Me.Label8.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'Label9
        '
        Me.Label9.Location = New System.Drawing.Point(16, 224)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(112, 23)
        Me.Label9.TabIndex = 11
        Me.Label9.Text = "Note:"
        Me.Label9.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.RadioButtonLandscape)
        Me.GroupBox1.Controls.Add(Me.RadioButtonPortrait)
        Me.GroupBox1.Controls.Add(Me.CheckBoxPrintPreview)
        Me.GroupBox1.Controls.Add(Me.CheckBoxLabelInBold)
        Me.GroupBox1.Controls.Add(Me.CheckBoxTabControlBoxed)
        Me.GroupBox1.Controls.Add(Me.CheckBoxTextBoxBoxed)
        Me.GroupBox1.Controls.Add(Me.ButtonPrintMe)
        Me.GroupBox1.Location = New System.Drawing.Point(8, 312)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(408, 88)
        Me.GroupBox1.TabIndex = 13
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Print Me - Choose options..."
        '
        'RadioButtonLandscape
        '
        Me.RadioButtonLandscape.Location = New System.Drawing.Point(320, 48)
        Me.RadioButtonLandscape.Name = "RadioButtonLandscape"
        Me.RadioButtonLandscape.Size = New System.Drawing.Size(80, 24)
        Me.RadioButtonLandscape.TabIndex = 10
        Me.RadioButtonLandscape.Text = "Landscape"
        '
        'RadioButtonPortrait
        '
        Me.RadioButtonPortrait.Location = New System.Drawing.Point(320, 24)
        Me.RadioButtonPortrait.Name = "RadioButtonPortrait"
        Me.RadioButtonPortrait.Size = New System.Drawing.Size(72, 24)
        Me.RadioButtonPortrait.TabIndex = 9
        Me.RadioButtonPortrait.Text = "Portrait"
        '
        'CheckBoxPrintPreview
        '
        Me.CheckBoxPrintPreview.Checked = True
        Me.CheckBoxPrintPreview.CheckState = System.Windows.Forms.CheckState.Checked
        Me.CheckBoxPrintPreview.Location = New System.Drawing.Point(208, 48)
        Me.CheckBoxPrintPreview.Name = "CheckBoxPrintPreview"
        Me.CheckBoxPrintPreview.Size = New System.Drawing.Size(120, 24)
        Me.CheckBoxPrintPreview.TabIndex = 8
        Me.CheckBoxPrintPreview.Text = "Print Preview"
        '
        'CheckBoxLabelInBold
        '
        Me.CheckBoxLabelInBold.Checked = True
        Me.CheckBoxLabelInBold.CheckState = System.Windows.Forms.CheckState.Checked
        Me.CheckBoxLabelInBold.Location = New System.Drawing.Point(208, 24)
        Me.CheckBoxLabelInBold.Name = "CheckBoxLabelInBold"
        Me.CheckBoxLabelInBold.TabIndex = 7
        Me.CheckBoxLabelInBold.Text = "Label in bold"
        '
        'CheckBoxTabControlBoxed
        '
        Me.CheckBoxTabControlBoxed.Location = New System.Drawing.Point(88, 48)
        Me.CheckBoxTabControlBoxed.Name = "CheckBoxTabControlBoxed"
        Me.CheckBoxTabControlBoxed.Size = New System.Drawing.Size(120, 24)
        Me.CheckBoxTabControlBoxed.TabIndex = 6
        Me.CheckBoxTabControlBoxed.Text = "TabControl Boxed"
        '
        'CheckBoxTextBoxBoxed
        '
        Me.CheckBoxTextBoxBoxed.Location = New System.Drawing.Point(88, 24)
        Me.CheckBoxTextBoxBoxed.Name = "CheckBoxTextBoxBoxed"
        Me.CheckBoxTextBoxBoxed.TabIndex = 5
        Me.CheckBoxTextBoxBoxed.Text = "TextBox Boxed"
        '
        'DemoForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(432, 413)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.TextBox4)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.DateTimePicker1)
        Me.Controls.Add(Me.TabControl1)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Label8)
        Me.Controls.Add(Me.Label9)
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "DemoForm"
        Me.Text = "This is my test Form"
        Me.TabControl1.ResumeLayout(False)
        Me.TabPage1.ResumeLayout(False)
        Me.TabPage2.ResumeLayout(False)
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.Panel1.ResumeLayout(False)
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub ButtonPrintMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPrintMe.Click

        'Create FormPrinting object
        Dim fp As New FormPrinting(Me)

        ' Set printing options
        fp.TextBoxBoxed = Me.CheckBoxTextBoxBoxed.Checked
        fp.TabControlBoxed = Me.CheckBoxTabControlBoxed.Checked
        fp.LabelInBold = Me.CheckBoxLabelInBold.Checked
        fp.PrintPreview = Me.CheckBoxPrintPreview.Checked

        fp.Orientation = FormPrinting.OrientationENum.Automatic
        If Me.RadioButtonPortrait.Checked Then
            fp.Orientation = FormPrinting.OrientationENum.Portrait
        ElseIf Me.RadioButtonLandscape.Checked Then
            fp.Orientation = FormPrinting.OrientationENum.Lanscape
        End If

        ' Print!
        fp.Print()

    End Sub
End Class
==========================
Class FormPrinting.vb
Imports System.drawing
Imports System.drawing.Printing

Public Class FormPrinting

    Public TextBoxBoxed As Boolean = False ' box around TextBox controls
    Public TabControlBoxed As Boolean = True ' box around TabControl controls
    Public LabelInBold As Boolean = True ' Print all labels in bold
    Public Orientation As OrientationENum = OrientationENum.Automatic ' choose print orientation (Automatic, Protrait or Landscape)
    Public PrintPreview As Boolean = True ' enabled Print preview instead of direct printing

    Public Enum OrientationENum
        Automatic = 1
        Portrait = 2
        Lanscape = 3
    End Enum


    Private _printFont As Font
    Private _Pen As New Pen(Color.Black)
    Private _Brush As Brush
    Private _f As System.Windows.forms.Form
    Private _TextBoxLikeControl As New ArrayList
    Private _yForm As Single
    Private _xform As Single

    Public Sub New(ByVal f As System.Windows.forms.Form)
        _f = f
        AddTextBoxLikeControl("ComboBox")
        AddTextBoxLikeControl("DateTimePicker")
        AddTextBoxLikeControl("DateTimeSlicker")
    End Sub

    Public Sub AddTextBoxLikeControl(ByVal stringType As String)
        _TextBoxLikeControl.Add(stringType)
    End Sub

    Public Sub Print()
        Try
            Dim pd As New PrintDocument
            pd.DocumentName = _f.Text
            ' Calculate Form position for printing
            Select Case Orientation
                Case OrientationENum.Automatic
                    If _f.Size.Width > (pd.DefaultPageSettings.Bounds.Width - pd.DefaultPageSettings.Margins.Right - pd.DefaultPageSettings.Margins.Left) Then
                        pd.DefaultPageSettings.Landscape() = True
                    End If
                Case OrientationENum.Lanscape
                    pd.DefaultPageSettings.Landscape() = True
                Case OrientationENum.Portrait
                    pd.DefaultPageSettings.Landscape() = False
            End Select
            _yForm = pd.DefaultPageSettings.Margins.Top
            _xform = (pd.DefaultPageSettings.Bounds.Width - _f.Size.Width) / 2
            AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
            If PrintPreview Then
                Dim pp As New PrintPreviewDialog
                pp.Document = pd
                pp.WindowState = FormWindowState.Maximized
                pp.ShowDialog()
            Else
                pd.Print()
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        _printFont = New Font(_f.Font.Name, CSng(_f.Font.Size * 1.2), FontStyle.Bold)
        ev.Graphics.DrawString(_f.Text, _printFont, Brushes.Black, _xform, _yForm)
        _yForm += _printFont.GetHeight(ev.Graphics)
        _Pen = New Pen(Color.Black, 2)
        _yForm += 1
        Dim points As PointF() = {New PointF(_xform, _yForm), New PointF(_xform + _f.Size.Width, _yForm)}
        ev.Graphics.DrawLines(_Pen, points)
        _yForm += _Pen.Width + 1

        ' Print each control on the form
        PrintControls(_f, ev, _xform, _yForm)
    End Sub

    Public Sub PrintControl(ByVal c As System.Windows.forms.Control, ByVal ev As PrintPageEventArgs, ByVal x As Single, ByVal y As Single)
        If c.Visible = True Then
            PrintOneControl(c, ev, x, y) ' Myself
            PrintControls(c, ev, x, y) 'Contained controls
        End If
    End Sub

    Public Sub PrintControls(ByVal c As System.Windows.forms.Control, ByVal ev As PrintPageEventArgs, ByVal x As Single, ByVal y As Single)
        For Each cIn As Control In c.Controls
            PrintControl(cIn, ev, x + cIn.Location.X, y + cIn.Location.Y)
        Next
    End Sub

    Public Sub PrintOneControl(ByVal c As System.Windows.forms.Control, ByVal ev As PrintPageEventArgs, ByVal x As Single, ByVal y As Single)
        ' Silver color if disable
        If c.Enabled Then
            _Pen = New Pen(Color.Black)
            _Brush = Brushes.Black
        Else
            _Pen = New Pen(Color.Silver)
            _Brush = Brushes.Silver
        End If

        Dim s As String = c.GetType.ToString
        Dim founded As Boolean = False
        'First check if it's a kind of text box
        If s.IndexOf("TextBox") >= 0 Then
            Try
                Dim tb As TextBox = c
                founded = True
                PrintText(c, ev, x, y, TextBoxBoxed, tb.Font.Bold, Not tb.Multiline And TextBoxBoxed, tb.TextAlign)
            Catch ex As Exception
                ' N'est pas un TextBox
            End Try
        End If
        'First check if it's a text box like control
        If Not founded Then
            For Each sType As String In _TextBoxLikeControl
                If s.IndexOf(sType) >= 0 Then
                    PrintText(c, ev, x, y, TextBoxBoxed, False, TextBoxBoxed, HorizontalAlignment.Left)
                    founded = True
                    Exit For
                End If
            Next
        End If
        'Process other type of control
        If Not founded Then
            Select Case c.GetType.ToString

                Case "System.Windows.Forms.Label"
                    Dim ha As HorizontalAlignment
                    Dim ss As String = c.Text
                    Dim ha2 As ContentAlignment = CType(c, Label).TextAlign
                    Select Case CType(c, Label).TextAlign

                        Case ContentAlignment.BottomLeft
                            ha = HorizontalAlignment.Left
                        Case ContentAlignment.TopLeft
                            ha = HorizontalAlignment.Left
                        Case ContentAlignment.MiddleLeft
                            ha = HorizontalAlignment.Left

                        Case ContentAlignment.BottomCenter
                            ha = HorizontalAlignment.Center
                        Case ContentAlignment.TopCenter
                            ha = HorizontalAlignment.Center
                        Case ContentAlignment.MiddleCenter
                            ha = HorizontalAlignment.Center

                        Case ContentAlignment.BottomRight
                            ha = HorizontalAlignment.Right
                        Case ContentAlignment.TopRight
                            ha = HorizontalAlignment.Right
                        Case ContentAlignment.MiddleRight
                            ha = HorizontalAlignment.Right
                    End Select
                    PrintText(c, ev, x, y, False, LabelInBold, False, ha)

                Case "System.Windows.Forms.CheckBox"
                    _printFont = New Font(c.Font.Name, c.Font.Size)
                    Dim w As Single = _printFont.GetHeight(ev.Graphics)
                    ev.Graphics.DrawRectangle(_Pen, x, y, w, w)
                    If CType(c, CheckBox).Checked Then
                        Dim d As Single = 3
                        Dim points1 As PointF() = {New PointF(x + d, y + d), New PointF(x + w - d, y + w - d)}
                        ev.Graphics.DrawLines(_Pen, points1)
                        Dim points2 As PointF() = {New PointF(x + w - d, y + d), New PointF(x + d, y + w - d)}
                        ev.Graphics.DrawLines(_Pen, points2)
                    End If
                    PrintText(c, ev, x + (w * 1.4), y - 2, False, False, False, HorizontalAlignment.Left)
                Case "System.Windows.Forms.RadioButton"

                    _printFont = New Font(c.Font.Name, c.Font.Size)
                    Dim w As Single = _printFont.GetHeight(ev.Graphics)
                    ev.Graphics.DrawEllipse(_Pen, x, y, w, w)
                    If CType(c, RadioButton).Checked Then
                        Dim d As Single = 3
                        ev.Graphics.FillEllipse(_Brush, x + d, y + d, w - d - d, w - d - d)
                    End If
                    PrintText(c, ev, x + (w * 1.4), y - 2, False, False, False, HorizontalAlignment.Left)
                Case "System.Windows.Forms.Panel"

                    If CType(c, System.Windows.Forms.Panel).BorderStyle <> BorderStyle.None Then
                        If CType(c, System.Windows.Forms.Panel).BorderStyle = BorderStyle.Fixed3D Then
                            _Pen = New Pen(Color.Silver)
                        End If
                        ev.Graphics.DrawRectangle(_Pen, x, y, c.Width, c.Height)
                    End If

                Case "System.Windows.Forms.GroupBox"
                    _Pen = New Pen(Color.Silver)
                    Dim w As Single = _printFont.GetHeight(ev.Graphics)
                    ev.Graphics.DrawRectangle(_Pen, x, y + w - w, c.Width, c.Height - w + w)
                    PrintText(c, ev, x + w, y, False, True, False, HorizontalAlignment.Left)

                Case "System.Windows.Forms.TabControl"
                    Dim tc As System.Windows.Forms.TabControl = c
                    _Pen = New Pen(Color.Gray)
                    If TabControlBoxed Then
                        ev.Graphics.DrawRectangle(_Pen, x, y, c.Width, c.Height)
                    End If
                    Dim points As PointF() = {New PointF(x, y + tc.ItemSize.Height), New PointF(x + tc.Width, y + tc.ItemSize.Height)}
                    ev.Graphics.DrawLines(_Pen, points)
                    Dim tp As System.Windows.forms.TabPage = tc.SelectedTab
                    'Nom du TabPage
                    _printFont = New Font(c.Font.Name, c.Font.Size, FontStyle.Bold)
                    ev.Graphics.DrawString(tp.Text, _printFont, Brushes.Black, x, y + (tc.ItemSize.Height - _printFont.GetHeight(ev.Graphics)) / 2)

                Case "System.Windows.Forms.PictureBox"
                    Dim pic As PictureBox = c
                    ev.Graphics.DrawImage(pic.Image, x, y, c.Width, c.Height)
            End Select
        End If
    End Sub

    Public Sub PrintText(ByVal c As System.Windows.forms.Control, ByVal ev As PrintPageEventArgs, ByVal x As Single, ByVal y As Single, ByVal tbBoxed As Boolean, ByVal inBold As Boolean, ByVal verticalCentering As Boolean, ByVal hAlignment As HorizontalAlignment)
        Dim r As RectangleF
        r.X = x
        r.Y = y
        r.Width = c.Width
        r.Height = c.Height

        'Box
        If tbBoxed Then
            ev.Graphics.DrawRectangle(_Pen, r.X, r.Y, r.Width, r.Height)
        End If

        'Text
        If inBold Then
            _printFont = New Font(c.Font.Name, c.Font.Size, FontStyle.Bold)
        Else
            _printFont = New Font(c.Font.Name, c.Font.Size)
        End If

        If verticalCentering Then
            Dim fontHeight As Single = _printFont.GetHeight(ev.Graphics)
            Dim deltaHeight As Single = (r.Height - fontHeight) / 2
            r.Y += deltaHeight
        Else
            r.Y += 2
        End If

        Dim drawFormat As New StringFormat
        Select Case hAlignment
            Case HorizontalAlignment.Left
                drawFormat.Alignment = StringAlignment.Near
            Case HorizontalAlignment.Center
                drawFormat.Alignment = StringAlignment.Center
            Case HorizontalAlignment.Right
                drawFormat.Alignment = StringAlignment.Far
        End Select
        ev.Graphics.DrawString(c.Text, _printFont, _Brush, r, drawFormat)
    End Sub
End Class
Avatar of Barkis_

ASKER

I tried both your codes, but with the same result:
"object reference not set to an instance of an object"

* the page setup show correctly (the "printer" buton is greyd out though"
* the print preview shows ok, but when pushing print the error shows.

I tried adding a new class and pasted Iboutchkine's code for the class in there.
and I used all your other code to create controls on my form, but still it says the above error when trying to print

And I get two  syntax errors in my code on similar code, the 'As' in this line produces a syntax error
For Each sType As String In _TextBoxLikeContro

I did't mention it was vb.net i was talking about, but Iguess you fgured that out :-)

Ideas?

/Barkis_
SOLUTION
Avatar of iboutchkine
iboutchkine

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>  And I get two  syntax errors in my code on similar code, the 'As' in this line produces a syntax error
>  For Each sType As String In _TextBoxLikeContro

This might happen if you're using the old .NET Framework 1.0 and/or Visual Studio 7.0 (2002) instead of the newer stuff.

The equivalent would be:

Dim sType As String
For Each sType In _TextBoxLikeContro
Avatar of Barkis_

ASKER

ah! ok.. thanks a bunch.

I'll give this a go a little later.

Barkis_
Avatar of Barkis_

ASKER

I have not forgotten about you yet, I have not found the time to try it out just yet.
I'll get on it as soon as iÍ can...
/Barkis_
Cool, let me know...

DCD
Avatar of Barkis_

ASKER

Thanks both of you, you have given me a lot of code to break down. I got DCD's last code to work and I will get iboutchkines code to work.
But I still don't understand how it all connects :-/
If either of you can just explain how and if I can make my PrintDialogue print my form without showing the printpreview
Or if you could help me with activating the Printer button on a standard pagesetup dialog.
I would very much appreciate it.
/Barkis_