Solved

How do I print contents of a listbox?

Posted on 2011-03-21
2
1,227 Views
Last Modified: 2012-05-11
How do I print contents of a listbox?
This is what I have, but get error messages:

 Dim streamToPrint As StreamReader

        Try
            streamToPrint = New StreamReader(Me.ListBoxAlpha.Items.ToString)
            Dim pd As New PrintDocument()
0
Comment
Question by:gs99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 125 total points
ID: 35184614
You would do something along these lines:
Public Class Form1

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

        If pdlg.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then ' Let user pick printer
            Dim pd As New Printing.PrintDocument()

            pd.PrinterSettings = pdlg.PrinterSettings               ' The document should print how the user set it up in the dialog
            pd.OriginAtMargins = True                               ' Not necessary, but prettier
            AddHandler pd.PrintPage, AddressOf Document_IsPrinting  ' PrintPage is where the "printing" actually occurs
            pd.Print()
        End If
    End Sub

    Private Sub Document_IsPrinting(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs)
        Dim f As New Font("Arial", 10)
        Dim vOffset As SizeF

        For Each item As String In Me.ListBoxAlpha.Items
            vOffset += e.Graphics.MeasureString(item, f, 300)   ' Get the height of the string as it would be on the page; accumulate so text doesn't overlap
            e.Graphics.DrawString(item, f, Brushes.Black, 0, vOffset.Height + 5)    ' Draw the string on the page
        Next
    End Sub
End Class

Open in new window

untitled.PNG
untitled6.PNG
0
 

Author Closing Comment

by:gs99
ID: 35185134
thanks for your quick reply
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question