Solved

How do I print contents of a listbox?

Posted on 2011-03-21
2
1,181 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
2 Comments
 
LVL 74

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now