Barkis_
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.PrintRang e.AllPages
PrintDialog1.PrinterSettin gs = prnsettings
PrintDialog1.ShowDialog()
End Sub
What am I missing?
Cheers
Barkis_
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.PrintRang
PrintDialog1.PrinterSettin
PrintDialog1.ShowDialog()
End Sub
What am I missing?
Cheers
Barkis_
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.ICon tainer
'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.TextB ox
Friend WithEvents TabControl1 As System.Windows.Forms.TabCo ntrol
Friend WithEvents TabPage1 As System.Windows.Forms.TabPa ge
Friend WithEvents TabPage2 As System.Windows.Forms.TabPa ge
Friend WithEvents TextBox2 As System.Windows.Forms.TextB ox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox3 As System.Windows.Forms.TextB ox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Butto n
Friend WithEvents DataGrid1 As System.Windows.Forms.DataG rid
Friend WithEvents ButtonPrintMe As System.Windows.Forms.Butto n
Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateT imePicker
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents TextBox4 As System.Windows.Forms.TextB ox
Friend WithEvents Label5Hide As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents ComboBox1 As System.Windows.Forms.Combo Box
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.Pictu reBox
Friend WithEvents GroupBox1 As System.Windows.Forms.Group Box
Friend WithEvents CheckBoxTextBoxBoxed As System.Windows.Forms.Check Box
Friend WithEvents CheckBoxTabControlBoxed As System.Windows.Forms.Check Box
Friend WithEvents CheckBoxPrintPreview As System.Windows.Forms.Check Box
Friend WithEvents CheckBoxLabelInBold As System.Windows.Forms.Check Box
Friend WithEvents RadioButtonPortrait As System.Windows.Forms.Radio Button
Friend WithEvents RadioButtonLandscape As System.Windows.Forms.Radio Button
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceM anager = New System.Resources.ResourceM anager(Get Type(DemoF orm))
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextB ox
Me.TabControl1 = New System.Windows.Forms.TabCo ntrol
Me.TabPage1 = New System.Windows.Forms.TabPa ge
Me.Label7 = New System.Windows.Forms.Label
Me.ComboBox1 = New System.Windows.Forms.Combo Box
Me.Label5 = New System.Windows.Forms.Label
Me.Button1 = New System.Windows.Forms.Butto n
Me.TextBox2 = New System.Windows.Forms.TextB ox
Me.Label2 = New System.Windows.Forms.Label
Me.TabPage2 = New System.Windows.Forms.TabPa ge
Me.DataGrid1 = New System.Windows.Forms.DataG rid
Me.TextBox3 = New System.Windows.Forms.TextB ox
Me.Label3 = New System.Windows.Forms.Label
Me.PictureBox1 = New System.Windows.Forms.Pictu reBox
Me.ButtonPrintMe = New System.Windows.Forms.Butto n
Me.DateTimePicker1 = New System.Windows.Forms.DateT imePicker
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.TextB ox
Me.Label8 = New System.Windows.Forms.Label
Me.Label9 = New System.Windows.Forms.Label
Me.GroupBox1 = New System.Windows.Forms.Group Box
Me.RadioButtonLandscape = New System.Windows.Forms.Radio Button
Me.RadioButtonPortrait = New System.Windows.Forms.Radio Button
Me.CheckBoxPrintPreview = New System.Windows.Forms.Check Box
Me.CheckBoxLabelInBold = New System.Windows.Forms.Check Box
Me.CheckBoxTabControlBoxed = New System.Windows.Forms.Check Box
Me.CheckBoxTextBoxBoxed = New System.Windows.Forms.Check Box
Me.TabControl1.SuspendLayo ut()
Me.TabPage1.SuspendLayout( )
Me.TabPage2.SuspendLayout( )
CType(Me.DataGrid1, System.ComponentModel.ISup portInitia lize).Begi nInit()
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.Ad d(Me.TabPa ge1)
Me.TabControl1.Controls.Ad d(Me.TabPa ge2)
Me.TabControl1.ItemSize = New System.Drawing.Size(62, 30)
Me.TabControl1.Location = New System.Drawing.Point(16, 48)
Me.TabControl1.Name = "TabControl1"
Me.TabControl1.SelectedInd ex = 0
Me.TabControl1.Size = New System.Drawing.Size(400, 144)
Me.TabControl1.TabIndex = 2
'
'TabPage1
'
Me.TabPage1.Controls.Add(M e.Label7)
Me.TabPage1.Controls.Add(M e.ComboBox 1)
Me.TabPage1.Controls.Add(M e.Label5)
Me.TabPage1.Controls.Add(M e.Button1)
Me.TabPage1.Controls.Add(M e.TextBox2 )
Me.TabPage1.Controls.Add(M e.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.ContentAlig nment.Midd leRight
'
'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("Micro soft Sans Serif", 11.0!, System.Drawing.FontStyle.R egular, System.Drawing.GraphicsUni t.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.ContentAlig nment.Midd leCenter
'
'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.Horiz ontalAlign ment.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.ContentAlig nment.Midd leRight
'
'TabPage2
'
Me.TabPage2.Controls.Add(M e.DataGrid 1)
Me.TabPage2.Controls.Add(M e.TextBox3 )
Me.TabPage2.Controls.Add(M e.Label3)
Me.TabPage2.Controls.Add(M e.PictureB ox1)
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.HeaderForeCol or = System.Drawing.SystemColor s.ControlT ext
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.Horiz ontalAlign ment.Cente r
'
'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( "PictureBo x1.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.Pictu reBoxSizeM ode.Stretc hImage
Me.PictureBox1.TabIndex = 12
Me.PictureBox1.TabStop = False
'
'ButtonPrintMe
'
Me.ButtonPrintMe.Font = New System.Drawing.Font("Micro soft Sans Serif", 10.0!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.ButtonPrintMe.ForeColor = System.Drawing.SystemColor s.Highligh t
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.Locatio n = New System.Drawing.Point(56, 200)
Me.DateTimePicker1.Name = "DateTimePicker1"
Me.DateTimePicker1.Size = New System.Drawing.Size(160, 20)
Me.DateTimePicker1.TabInde x = 7
'
'Panel1
'
Me.Panel1.BorderStyle = System.Windows.Forms.Borde rStyle.Fix ed3D
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.ContentAlig nment.Midd leLeft
'
'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.ContentAlig nment.Midd leLeft
'
'GroupBox1
'
Me.GroupBox1.Controls.Add( Me.RadioBu ttonLandsc ape)
Me.GroupBox1.Controls.Add( Me.RadioBu ttonPortra it)
Me.GroupBox1.Controls.Add( Me.CheckBo xPrintPrev iew)
Me.GroupBox1.Controls.Add( Me.CheckBo xLabelInBo ld)
Me.GroupBox1.Controls.Add( Me.CheckBo xTabContro lBoxed)
Me.GroupBox1.Controls.Add( Me.CheckBo xTextBoxBo xed)
Me.GroupBox1.Controls.Add( Me.ButtonP rintMe)
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.Lo cation = New System.Drawing.Point(320, 48)
Me.RadioButtonLandscape.Na me = "RadioButtonLandscape"
Me.RadioButtonLandscape.Si ze = New System.Drawing.Size(80, 24)
Me.RadioButtonLandscape.Ta bIndex = 10
Me.RadioButtonLandscape.Te xt = "Landscape"
'
'RadioButtonPortrait
'
Me.RadioButtonPortrait.Loc ation = New System.Drawing.Point(320, 24)
Me.RadioButtonPortrait.Nam e = "RadioButtonPortrait"
Me.RadioButtonPortrait.Siz e = New System.Drawing.Size(72, 24)
Me.RadioButtonPortrait.Tab Index = 9
Me.RadioButtonPortrait.Tex t = "Portrait"
'
'CheckBoxPrintPreview
'
Me.CheckBoxPrintPreview.Ch ecked = True
Me.CheckBoxPrintPreview.Ch eckState = System.Windows.Forms.Check State.Chec ked
Me.CheckBoxPrintPreview.Lo cation = New System.Drawing.Point(208, 48)
Me.CheckBoxPrintPreview.Na me = "CheckBoxPrintPreview"
Me.CheckBoxPrintPreview.Si ze = New System.Drawing.Size(120, 24)
Me.CheckBoxPrintPreview.Ta bIndex = 8
Me.CheckBoxPrintPreview.Te xt = "Print Preview"
'
'CheckBoxLabelInBold
'
Me.CheckBoxLabelInBold.Che cked = True
Me.CheckBoxLabelInBold.Che ckState = System.Windows.Forms.Check State.Chec ked
Me.CheckBoxLabelInBold.Loc ation = New System.Drawing.Point(208, 24)
Me.CheckBoxLabelInBold.Nam e = "CheckBoxLabelInBold"
Me.CheckBoxLabelInBold.Tab Index = 7
Me.CheckBoxLabelInBold.Tex t = "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.Lo cation = New System.Drawing.Point(88, 24)
Me.CheckBoxTextBoxBoxed.Na me = "CheckBoxTextBoxBoxed"
Me.CheckBoxTextBoxBoxed.Ta bIndex = 5
Me.CheckBoxTextBoxBoxed.Te xt = "TextBox Boxed"
'
'DemoForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(432, 413)
Me.Controls.Add(Me.GroupBo x1)
Me.Controls.Add(Me.TextBox 4)
Me.Controls.Add(Me.TextBox 1)
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.DateTim ePicker1)
Me.Controls.Add(Me.TabCont rol1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Label8)
Me.Controls.Add(Me.Label9)
Me.Icon = CType(resources.GetObject( "$this.Ico n"), System.Drawing.Icon)
Me.Name = "DemoForm"
Me.Text = "This is my test Form"
Me.TabControl1.ResumeLayou t(False)
Me.TabPage1.ResumeLayout(F alse)
Me.TabPage2.ResumeLayout(F alse)
CType(Me.DataGrid1, System.ComponentModel.ISup portInitia lize).EndI nit()
Me.Panel1.ResumeLayout(Fal se)
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.Ch ecked
fp.TabControlBoxed = Me.CheckBoxTabControlBoxed .Checked
fp.LabelInBold = Me.CheckBoxLabelInBold.Che cked
fp.PrintPreview = Me.CheckBoxPrintPreview.Ch ecked
fp.Orientation = FormPrinting.OrientationEN um.Automat ic
If Me.RadioButtonPortrait.Che cked Then
fp.Orientation = FormPrinting.OrientationEN um.Portrai t
ElseIf Me.RadioButtonLandscape.Ch ecked Then
fp.Orientation = FormPrinting.OrientationEN um.Lanscap e
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("Com boBox")
AddTextBoxLikeControl("Dat eTimePicke r")
AddTextBoxLikeControl("Dat eTimeSlick er")
End Sub
Public Sub AddTextBoxLikeControl(ByVa l stringType As String)
_TextBoxLikeControl.Add(st ringType)
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.Bo unds.Width - pd.DefaultPageSettings.Mar gins.Right - pd.DefaultPageSettings.Mar gins.Left) Then
pd.DefaultPageSettings.Lan dscape() = True
End If
Case OrientationENum.Lanscape
pd.DefaultPageSettings.Lan dscape() = True
Case OrientationENum.Portrait
pd.DefaultPageSettings.Lan dscape() = False
End Select
_yForm = pd.DefaultPageSettings.Mar gins.Top
_xform = (pd.DefaultPageSettings.Bo unds.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.Gr aphics)
_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.Contr ol, 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.Contr ol, 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.Contr ol, 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.Labe l"
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.BottomLef t
ha = HorizontalAlignment.Left
Case ContentAlignment.TopLeft
ha = HorizontalAlignment.Left
Case ContentAlignment.MiddleLef t
ha = HorizontalAlignment.Left
Case ContentAlignment.BottomCen ter
ha = HorizontalAlignment.Center
Case ContentAlignment.TopCenter
ha = HorizontalAlignment.Center
Case ContentAlignment.MiddleCen ter
ha = HorizontalAlignment.Center
Case ContentAlignment.BottomRig ht
ha = HorizontalAlignment.Right
Case ContentAlignment.TopRight
ha = HorizontalAlignment.Right
Case ContentAlignment.MiddleRig ht
ha = HorizontalAlignment.Right
End Select
PrintText(c, ev, x, y, False, LabelInBold, False, ha)
Case "System.Windows.Forms.Chec kBox"
_printFont = New Font(c.Font.Name, c.Font.Size)
Dim w As Single = _printFont.GetHeight(ev.Gr aphics)
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.Radi oButton"
_printFont = New Font(c.Font.Name, c.Font.Size)
Dim w As Single = _printFont.GetHeight(ev.Gr aphics)
ev.Graphics.DrawEllipse(_P en, x, y, w, w)
If CType(c, RadioButton).Checked Then
Dim d As Single = 3
ev.Graphics.FillEllipse(_B rush, 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.Pane l"
If CType(c, System.Windows.Forms.Panel ).BorderSt yle <> BorderStyle.None Then
If CType(c, System.Windows.Forms.Panel ).BorderSt yle = 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.Grou pBox"
_Pen = New Pen(Color.Silver)
Dim w As Single = _printFont.GetHeight(ev.Gr aphics)
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.TabC ontrol"
Dim tc As System.Windows.Forms.TabCo ntrol = 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.TabPa ge = 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.Gr aphics)) / 2)
Case "System.Windows.Forms.Pict ureBox"
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.Contr ol, 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.Gr aphics)
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.T ext, _printFont, _Brush, r, drawFormat)
End Sub
End Class
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.ICon
'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.TextB
Friend WithEvents TabControl1 As System.Windows.Forms.TabCo
Friend WithEvents TabPage1 As System.Windows.Forms.TabPa
Friend WithEvents TabPage2 As System.Windows.Forms.TabPa
Friend WithEvents TextBox2 As System.Windows.Forms.TextB
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox3 As System.Windows.Forms.TextB
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Butto
Friend WithEvents DataGrid1 As System.Windows.Forms.DataG
Friend WithEvents ButtonPrintMe As System.Windows.Forms.Butto
Friend WithEvents DateTimePicker1 As System.Windows.Forms.DateT
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents TextBox4 As System.Windows.Forms.TextB
Friend WithEvents Label5Hide As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents ComboBox1 As System.Windows.Forms.Combo
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.Pictu
Friend WithEvents GroupBox1 As System.Windows.Forms.Group
Friend WithEvents CheckBoxTextBoxBoxed As System.Windows.Forms.Check
Friend WithEvents CheckBoxTabControlBoxed As System.Windows.Forms.Check
Friend WithEvents CheckBoxPrintPreview As System.Windows.Forms.Check
Friend WithEvents CheckBoxLabelInBold As System.Windows.Forms.Check
Friend WithEvents RadioButtonPortrait As System.Windows.Forms.Radio
Friend WithEvents RadioButtonLandscape As System.Windows.Forms.Radio
<System.Diagnostics.Debugg
Dim resources As System.Resources.ResourceM
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextB
Me.TabControl1 = New System.Windows.Forms.TabCo
Me.TabPage1 = New System.Windows.Forms.TabPa
Me.Label7 = New System.Windows.Forms.Label
Me.ComboBox1 = New System.Windows.Forms.Combo
Me.Label5 = New System.Windows.Forms.Label
Me.Button1 = New System.Windows.Forms.Butto
Me.TextBox2 = New System.Windows.Forms.TextB
Me.Label2 = New System.Windows.Forms.Label
Me.TabPage2 = New System.Windows.Forms.TabPa
Me.DataGrid1 = New System.Windows.Forms.DataG
Me.TextBox3 = New System.Windows.Forms.TextB
Me.Label3 = New System.Windows.Forms.Label
Me.PictureBox1 = New System.Windows.Forms.Pictu
Me.ButtonPrintMe = New System.Windows.Forms.Butto
Me.DateTimePicker1 = New System.Windows.Forms.DateT
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.TextB
Me.Label8 = New System.Windows.Forms.Label
Me.Label9 = New System.Windows.Forms.Label
Me.GroupBox1 = New System.Windows.Forms.Group
Me.RadioButtonLandscape = New System.Windows.Forms.Radio
Me.RadioButtonPortrait = New System.Windows.Forms.Radio
Me.CheckBoxPrintPreview = New System.Windows.Forms.Check
Me.CheckBoxLabelInBold = New System.Windows.Forms.Check
Me.CheckBoxTabControlBoxed
Me.CheckBoxTextBoxBoxed = New System.Windows.Forms.Check
Me.TabControl1.SuspendLayo
Me.TabPage1.SuspendLayout(
Me.TabPage2.SuspendLayout(
CType(Me.DataGrid1, System.ComponentModel.ISup
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.Ad
Me.TabControl1.Controls.Ad
Me.TabControl1.ItemSize = New System.Drawing.Size(62, 30)
Me.TabControl1.Location = New System.Drawing.Point(16, 48)
Me.TabControl1.Name = "TabControl1"
Me.TabControl1.SelectedInd
Me.TabControl1.Size = New System.Drawing.Size(400, 144)
Me.TabControl1.TabIndex = 2
'
'TabPage1
'
Me.TabPage1.Controls.Add(M
Me.TabPage1.Controls.Add(M
Me.TabPage1.Controls.Add(M
Me.TabPage1.Controls.Add(M
Me.TabPage1.Controls.Add(M
Me.TabPage1.Controls.Add(M
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.ContentAlig
'
'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("Micro
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.ContentAlig
'
'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.Horiz
'
'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.ContentAlig
'
'TabPage2
'
Me.TabPage2.Controls.Add(M
Me.TabPage2.Controls.Add(M
Me.TabPage2.Controls.Add(M
Me.TabPage2.Controls.Add(M
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.HeaderForeCol
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.Horiz
'
'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(
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.Pictu
Me.PictureBox1.TabIndex = 12
Me.PictureBox1.TabStop = False
'
'ButtonPrintMe
'
Me.ButtonPrintMe.Font = New System.Drawing.Font("Micro
Me.ButtonPrintMe.ForeColor
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.Locatio
Me.DateTimePicker1.Name = "DateTimePicker1"
Me.DateTimePicker1.Size = New System.Drawing.Size(160, 20)
Me.DateTimePicker1.TabInde
'
'Panel1
'
Me.Panel1.BorderStyle = System.Windows.Forms.Borde
Me.Panel1.Controls.Add(Me.
Me.Panel1.Controls.Add(Me.
Me.Panel1.Controls.Add(Me.
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.ContentAlig
'
'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.ContentAlig
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
Me.GroupBox1.Controls.Add(
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.Lo
Me.RadioButtonLandscape.Na
Me.RadioButtonLandscape.Si
Me.RadioButtonLandscape.Ta
Me.RadioButtonLandscape.Te
'
'RadioButtonPortrait
'
Me.RadioButtonPortrait.Loc
Me.RadioButtonPortrait.Nam
Me.RadioButtonPortrait.Siz
Me.RadioButtonPortrait.Tab
Me.RadioButtonPortrait.Tex
'
'CheckBoxPrintPreview
'
Me.CheckBoxPrintPreview.Ch
Me.CheckBoxPrintPreview.Ch
Me.CheckBoxPrintPreview.Lo
Me.CheckBoxPrintPreview.Na
Me.CheckBoxPrintPreview.Si
Me.CheckBoxPrintPreview.Ta
Me.CheckBoxPrintPreview.Te
'
'CheckBoxLabelInBold
'
Me.CheckBoxLabelInBold.Che
Me.CheckBoxLabelInBold.Che
Me.CheckBoxLabelInBold.Loc
Me.CheckBoxLabelInBold.Nam
Me.CheckBoxLabelInBold.Tab
Me.CheckBoxLabelInBold.Tex
'
'CheckBoxTabControlBoxed
'
Me.CheckBoxTabControlBoxed
Me.CheckBoxTabControlBoxed
Me.CheckBoxTabControlBoxed
Me.CheckBoxTabControlBoxed
Me.CheckBoxTabControlBoxed
'
'CheckBoxTextBoxBoxed
'
Me.CheckBoxTextBoxBoxed.Lo
Me.CheckBoxTextBoxBoxed.Na
Me.CheckBoxTextBoxBoxed.Ta
Me.CheckBoxTextBoxBoxed.Te
'
'DemoForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(432, 413)
Me.Controls.Add(Me.GroupBo
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.DateTim
Me.Controls.Add(Me.TabCont
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Label8)
Me.Controls.Add(Me.Label9)
Me.Icon = CType(resources.GetObject(
Me.Name = "DemoForm"
Me.Text = "This is my test Form"
Me.TabControl1.ResumeLayou
Me.TabPage1.ResumeLayout(F
Me.TabPage2.ResumeLayout(F
CType(Me.DataGrid1, System.ComponentModel.ISup
Me.Panel1.ResumeLayout(Fal
Me.GroupBox1.ResumeLayout(
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.Ch
fp.TabControlBoxed = Me.CheckBoxTabControlBoxed
fp.LabelInBold = Me.CheckBoxLabelInBold.Che
fp.PrintPreview = Me.CheckBoxPrintPreview.Ch
fp.Orientation = FormPrinting.OrientationEN
If Me.RadioButtonPortrait.Che
fp.Orientation = FormPrinting.OrientationEN
ElseIf Me.RadioButtonLandscape.Ch
fp.Orientation = FormPrinting.OrientationEN
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("Com
AddTextBoxLikeControl("Dat
AddTextBoxLikeControl("Dat
End Sub
Public Sub AddTextBoxLikeControl(ByVa
_TextBoxLikeControl.Add(st
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.Bo
pd.DefaultPageSettings.Lan
End If
Case OrientationENum.Lanscape
pd.DefaultPageSettings.Lan
Case OrientationENum.Portrait
pd.DefaultPageSettings.Lan
End Select
_yForm = pd.DefaultPageSettings.Mar
_xform = (pd.DefaultPageSettings.Bo
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.
_yForm += _printFont.GetHeight(ev.Gr
_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
_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.Contr
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.Contr
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.Contr
' 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.Labe
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.BottomLef
ha = HorizontalAlignment.Left
Case ContentAlignment.TopLeft
ha = HorizontalAlignment.Left
Case ContentAlignment.MiddleLef
ha = HorizontalAlignment.Left
Case ContentAlignment.BottomCen
ha = HorizontalAlignment.Center
Case ContentAlignment.TopCenter
ha = HorizontalAlignment.Center
Case ContentAlignment.MiddleCen
ha = HorizontalAlignment.Center
Case ContentAlignment.BottomRig
ha = HorizontalAlignment.Right
Case ContentAlignment.TopRight
ha = HorizontalAlignment.Right
Case ContentAlignment.MiddleRig
ha = HorizontalAlignment.Right
End Select
PrintText(c, ev, x, y, False, LabelInBold, False, ha)
Case "System.Windows.Forms.Chec
_printFont = New Font(c.Font.Name, c.Font.Size)
Dim w As Single = _printFont.GetHeight(ev.Gr
ev.Graphics.DrawRectangle(
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
Dim points2 As PointF() = {New PointF(x + w - d, y + d), New PointF(x + d, y + w - d)}
ev.Graphics.DrawLines(_Pen
End If
PrintText(c, ev, x + (w * 1.4), y - 2, False, False, False, HorizontalAlignment.Left)
Case "System.Windows.Forms.Radi
_printFont = New Font(c.Font.Name, c.Font.Size)
Dim w As Single = _printFont.GetHeight(ev.Gr
ev.Graphics.DrawEllipse(_P
If CType(c, RadioButton).Checked Then
Dim d As Single = 3
ev.Graphics.FillEllipse(_B
End If
PrintText(c, ev, x + (w * 1.4), y - 2, False, False, False, HorizontalAlignment.Left)
Case "System.Windows.Forms.Pane
If CType(c, System.Windows.Forms.Panel
If CType(c, System.Windows.Forms.Panel
_Pen = New Pen(Color.Silver)
End If
ev.Graphics.DrawRectangle(
End If
Case "System.Windows.Forms.Grou
_Pen = New Pen(Color.Silver)
Dim w As Single = _printFont.GetHeight(ev.Gr
ev.Graphics.DrawRectangle(
PrintText(c, ev, x + w, y, False, True, False, HorizontalAlignment.Left)
Case "System.Windows.Forms.TabC
Dim tc As System.Windows.Forms.TabCo
_Pen = New Pen(Color.Gray)
If TabControlBoxed Then
ev.Graphics.DrawRectangle(
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
Dim tp As System.Windows.forms.TabPa
'Nom du TabPage
_printFont = New Font(c.Font.Name, c.Font.Size, FontStyle.Bold)
ev.Graphics.DrawString(tp.
Case "System.Windows.Forms.Pict
Dim pic As PictureBox = c
ev.Graphics.DrawImage(pic.
End Select
End If
End Sub
Public Sub PrintText(ByVal c As System.Windows.forms.Contr
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(
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.Gr
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.T
End Sub
End Class
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_
"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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> 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
> 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
ASKER
ah! ok.. thanks a bunch.
I'll give this a go a little later.
Barkis_
I'll give this a go a little later.
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_
I'll get on it as soon as iÍ can...
/Barkis_
Cool, let me know...
DCD
DCD
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_
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_
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.Pr
Dim img As Image = bmp
Dim X, Y As Integer
X = Math.Max(0, (e.PageSettings.Bounds.Wid
Y = Math.Max(0, (e.PageSettings.Bounds.Hei
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