none
Formatting data read from a listbox to be printed RRS feed

  • Question

  • I'm reading data from a populated listbox. 

    This data is to be printed. 

    The problem is that the spacing on the screen isn't the same as it appears on the printout.  As of now, I have managed to get all the data from one line on one line.  The 1st line of a set of data is different from the lines below that line supporting the data in the 1st line.  The lines 2-n don't line up nicely as they did on the screen.  I've switched to a mono-spaced font which I thought would take care of the problem.  It didn't. Any suggestions would be appreciated.


    • Edited by rasimon27 Tuesday, January 9, 2018 9:46 PM
    Tuesday, January 9, 2018 9:45 PM

All replies

  • You will need to show your printpage event code and possibly an image of the form you want to duplicate.

    Tuesday, January 9, 2018 11:45 PM
  • Hello,

    Here is a conceptual (and fully functional) example. Load data into a ListBox via a List(Of T), in this case a List(Of Person). No need to set DisplayMember as I have overridden ToString. Why does this work? Well internally when setting the DataSource with no DisplayMember set the default is to use the ToString property (if we did this with a DataTable we would get DataRow for each row in the ListBox).

    Note the Read-Only property FullName, I have formatted it to place 10 spaces after FirstName which if we show this to the IDE output window we get these results. If we did 10 rather than -10 spaces are to the left of first name

    The class

    Public Class Person
        Public Property FirstName As String
        Public Property LastName As String
        Public ReadOnly Property FullName As String
            Get
                Return $"{FirstName,-10} {LastName}"
            End Get
        End Property
        Public Overrides Function ToString() As String
            Return $"{FirstName} {LastName}"
        End Function
    End Class

    Full code

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim peopleList As New List(Of Person) From
            {
                New Person With {.FirstName = "Karen", .LastName = "Payne"},
                New Person With {.FirstName = "Mary", .LastName = "Jenkins"},
                New Person With {.FirstName = "Tim", .LastName = "Hall"}
            }
            ListBox1.DataSource = peopleList
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            CType(ListBox1.DataSource, List(Of Person)).ForEach(Sub(p) Console.WriteLine(p.FullName))
        End Sub
    End Class
    
    Public Class Person
        Public Property FirstName As String
        Public Property LastName As String
        Public ReadOnly Property FullName As String
            Get
                Return $"{FirstName,-10} {LastName}"
            End Get
        End Property
        Public Overrides Function ToString() As String
            Return $"{FirstName} {LastName}"
        End Function
    End Class
    
    In the end, I would not compromise the ListBox display if it looks good for the sake of printing. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 10, 2018 1:03 AM
    Moderator