How can I print the contents of a Listbox using



How can I print the contents of a Listbox (items in the listbox) using

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ADawnAuthor Commented:
Bob LearnedCommented:
Do you know how to use a PrintDocument?

Bob LearnedCommented:
What version of .NET?  2002, 2003 or 2005?

ADawnAuthor Commented:
Sorry Guys,

TheLearnedOne, No I've never used PrintDocument. I'm programming with .Net 2003.

Bob LearnedCommented:
Here is a small example form with a PrintDocument, PrintPreviewControl, and a ListBox:

Public Class frmPrintListBox
  Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

  Public Sub New()

    'This call is required by the Windows Form Designer.

    '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
      End If
    End If
  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 PrintPreviewControl1 As System.Windows.Forms.PrintPreviewControl
  Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
  Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.PrintPreviewControl1 = New System.Windows.Forms.PrintPreviewControl
    Me.ListBox1 = New System.Windows.Forms.ListBox
    Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument
    Me.PrintPreviewControl1.AutoZoom = False
    Me.PrintPreviewControl1.Document = Me.PrintDocument1
    Me.PrintPreviewControl1.Location = New System.Drawing.Point(176, 16)
    Me.PrintPreviewControl1.Name = "PrintPreviewControl1"
    Me.PrintPreviewControl1.Size = New System.Drawing.Size(468, 376)
    Me.PrintPreviewControl1.TabIndex = 3
    Me.PrintPreviewControl1.Zoom = 0.3
    Me.ListBox1.Items.AddRange(New Object() {"1", "2", "3", "4", "5", "6"})
    Me.ListBox1.Location = New System.Drawing.Point(20, 16)
    Me.ListBox1.Name = "ListBox1"
    Me.ListBox1.Size = New System.Drawing.Size(120, 95)
    Me.ListBox1.TabIndex = 2
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(660, 410)
    Me.Name = "frmPrintListBox"
    Me.Text = "frmPrintListBox"

  End Sub

#End Region

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.PrintPreviewControl1.Zoom = 1

  End Sub

  Private m_currentIndex As Integer
  Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

    e.HasMorePages = True

    Dim bottomMargin As Integer = Me.PrintDocument1.DefaultPageSettings.Margins.Bottom
    Dim topMargin As Integer = Me.PrintDocument1.DefaultPageSettings.Margins.Top
    Dim leftMargin As Integer = Me.PrintDocument1.DefaultPageSettings.Margins.Left
    Dim rightMargin As Integer = Me.PrintDocument1.DefaultPageSettings.Margins.Right

    Dim pageHeight As Integer = Me.PrintDocument1.DefaultPageSettings.Bounds.Height - topMargin - bottomMargin
    Dim fontHeight As Integer = Me.ListBox1.Font.Height
    Dim itemCount As Integer = pageHeight / fontHeight
    Dim x As Integer = leftMargin + 10
    Dim y As Integer = topMargin

    For index As Integer = m_currentIndex To m_currentIndex + itemCount
      If index < Me.ListBox1.Items.Count Then
        e.Graphics.DrawString(Me.ListBox1.GetItemText(index), Me.ListBox1.Font, Brushes.Black, x, y)
        y += fontHeight
        e.HasMorePages = False
      End If
    Next index

  End Sub

End Class


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.