Solved

How do I print contents of a listbox?

Posted on 2011-03-21
2
1,204 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 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

825 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