Solved

How do I print contents of a listbox?

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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