Results 1 to 5 of 5

Thread: print listbox item in multiple pages

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2020
    Posts
    51

    print listbox item in multiple pages

    hi to everybody,

    i have to listboxes with different data.
    i need to print them but they can have one o several items inside.i dont' know in advance..
    i use this code


    Code:
    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim x As Integer
    e.HasMorePages = True
    Try
    Dim Font1 As New System.Drawing.Font("Verdana", 20, FontStyle.Italic)
    Dim Font2 As New System.Drawing.Font("Verdana", 12, FontStyle.Italic)
    Dim yPos As Long = 190
    Dim xPos As Long = 50
    e.Graphics.DrawString("DATI TRENO " & infotreno1, Font1, Brushes.Black, 50, 50)
    e.Graphics.DrawString("STAZIONE DI " & ComboBox1.SelectedValue, Font2, Brushes.Black, 50, 90)
    e.Graphics.DrawString(paxinsalita1 & " CLIENTI IN SALITA", Font2, Brushes.Black, 50, 110)
    e.Graphics.DrawString(paxindiscesa1 & " CLIENTI IN DISCESA", Font2, Brushes.Black, 50, 130)
    e.Graphics.DrawString("DESTINAZIONI IN SALITA DA " & ComboBox1.SelectedValue, Font2, Brushes.Black, 50, 170)
    For x = 0 To ListBox1.Items.Count - 1
    e.Graphics.DrawString(ListBox1.Items(x).ToString, Font2, Brushes.Black, xPos, yPos, New StringFormat())
    yPos = yPos + Font2.Height
    Next
    e.Graphics.DrawString("", Font2, Brushes.Black, xPos, yPos)
    yPos = yPos + Font2.Height
    
    e.Graphics.DrawString("DATI PRENOTAZIONI", Font2, Brushes.Black, xPos, yPos)
    yPos = yPos + Font2.Height
    
    
    For x = 0 To ListBox11.Items.Count - 1
    e.Graphics.DrawString(ListBox11.Items(x).ToString, Font2, Brushes.Black, xPos, yPos, New StringFormat())
    yPos = yPos + Font2.Height
    Next
    If x = ListBox11.Items.Count Then
    e.HasMorePages = False
    End If
    
    		Catch ex As Exception
    			MessageBox.Show(ex.Message & Environment.NewLine & ex.StackTrace)
    		End Try
    End Sub
    but it prints only one page,cutting the other data....
    what's wrong in my code?? i don't understand.
    thanks for you help

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: print listbox item in multiple pages

    Firstly, one of the main reasons for using CODE tags is that they maintain indenting and thus make your code easier to read. By pasting your code in such a way as to remove the indenting, you have made it significantly harder to read and, thus, significantly harder for us to help you. If you'd like strangers to volunteer their time to help, you should do everything you can to make it as easy for us to do so. Given that VS will format your code for you, there's little excuse for not posting formatted code. If you don't have access to VS, take the time to do it by hand.
    vb.net Code:
    1. Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.     Dim x As Integer
    3.  
    4.     e.HasMorePages = True
    5.  
    6.     Try
    7.         Dim Font1 As New System.Drawing.Font("Verdana", 20, FontStyle.Italic)
    8.         Dim Font2 As New System.Drawing.Font("Verdana", 12, FontStyle.Italic)
    9.         Dim yPos As Long = 190
    10.         Dim xPos As Long = 50
    11.  
    12.         e.Graphics.DrawString("DATI TRENO " & infotreno1, Font1, Brushes.Black, 50, 50)
    13.         e.Graphics.DrawString("STAZIONE DI " & ComboBox1.SelectedValue, Font2, Brushes.Black, 50, 90)
    14.         e.Graphics.DrawString(paxinsalita1 & " CLIENTI IN SALITA", Font2, Brushes.Black, 50, 110)
    15.         e.Graphics.DrawString(paxindiscesa1 & " CLIENTI IN DISCESA", Font2, Brushes.Black, 50, 130)
    16.         e.Graphics.DrawString("DESTINAZIONI IN SALITA DA " & ComboBox1.SelectedValue, Font2, Brushes.Black, 50, 170)
    17.  
    18.         For x = 0 To ListBox1.Items.Count - 1
    19.             e.Graphics.DrawString(ListBox1.Items(x).ToString, Font2, Brushes.Black, xPos, yPos, New StringFormat())
    20.             yPos = yPos + Font2.Height
    21.         Next
    22.  
    23.         e.Graphics.DrawString("", Font2, Brushes.Black, xPos, yPos)
    24.         yPos = yPos + Font2.Height
    25.  
    26.         e.Graphics.DrawString("DATI PRENOTAZIONI", Font2, Brushes.Black, xPos, yPos)
    27.         yPos = yPos + Font2.Height
    28.  
    29.         For x = 0 To ListBox11.Items.Count - 1
    30.             e.Graphics.DrawString(ListBox11.Items(x).ToString, Font2, Brushes.Black, xPos, yPos, New StringFormat())
    31.             yPos = yPos + Font2.Height
    32.         Next
    33.  
    34.         If x = ListBox11.Items.Count Then
    35.             e.HasMorePages = False
    36.         End If
    37.     Catch ex As Exception
    38.         MessageBox.Show(ex.Message & Environment.NewLine & ex.StackTrace)
    39.     End Try
    40. End Sub

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: print listbox item in multiple pages

    As for the problem, have you actually debugged your code? I don't mean just run the project but set a breakpoint on that method and then stepped through it line by line, examining the state at each step and comparing what actually happens to what you expect to happen? That is how you work out where your code is going wrong, not just by running the app and reading the code. If it were that easy then we wouldn't make mistakes in the first place.

    You need to ALWAYS debug your code first, before even considering posting a question here. If you haven't done so, do so now. It will likely become obvious what the problem is if you do because one would hope that you know exactly what each line of code is supposed to do and you will be able to see exactly when and where a line of code does something different. Even if you still can't solve the problem, at least you will be able to point us to exactly where in the code there is an issue and describe exactly what that issue is, including what data is in play at the time. If the actual behaviour never differs from the expected behaviour and you still don;t get the expected result then obviously your expectations are wrong and that is what you need to reevaluate.

    The issue, in principle at least, is pretty obvious. I'm guessing that you don't actually understand how the PrintDocument.PrintPage event works. The idea is that the event is raised and you execute drawing code for one printed page. You then set e.HasMorePages to True if there are more pages to print and then the event gets raised again. Etc. If the PrintPage event handler is supposed to print one page and you want only some of the items in your ListBox to be printed on that page, why would the code to print that page be looping through every item in the ListBox? If you loop through every item in the code that prints one page then of course you'll get every item printed on one page. Here is a simple example that would print five items per page that demonstrates the principle of paging. You can apply that principle as you see fit in any particular scenario.
    vb.net Code:
    1. Private Const ITEMS_PER_PAGE As Integer = 5
    2.  
    3. Private currentItemIndex As Integer
    4.  
    5. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    6.     PrintDocument1.Print()
    7. End Sub
    8.  
    9. Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
    10.     'Start at the first item for the new print run.
    11.     currentItemIndex = 0
    12. End Sub
    13.  
    14. Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    15.     Dim x = 100
    16.     Dim y = 100
    17.  
    18.     'Assume that there are more pages until we find that there are not.
    19.     e.HasMorePages = True
    20.  
    21.     'Only print the specified number of items on the current page.
    22.     For i = 1 To ITEMS_PER_PAGE
    23.         'Print the current item.
    24.         e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items(currentItemIndex)), Font, Brushes.Black, x, y)
    25.  
    26.         'Move to the next item.
    27.         currentItemIndex += 1
    28.  
    29.         If currentItemIndex = ListBox1.Items.Count Then
    30.             'We have gone past the last item so we are done.
    31.             e.HasMorePages = False
    32.             Exit For
    33.         End If
    34.  
    35.         y += 50
    36.     Next
    37. End Sub

  4. #4
    Lively Member
    Join Date
    Jan 2020
    Posts
    120

    Re: print listbox item in multiple pages

    In regards to actual printing, I simple set the DocumentName of the PrintDocument, there is of course more to do here so it actually prints.

    Code:
    Public Class SomeDemo
        Private dt As DataTable = LoadDataFromSomeplace()
        ''' <summary>
        ''' Load ListBox with mocked data
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub SomeDemo_Load(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) _
        Handles MyBase.Load
            For Each Row As DataRow In dt.Rows
                CheckedListBox1.Items.Add(Row.Field(Of String)("Text"))
            Next
        End Sub
        ''' <summary>
        ''' Get checked items
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub Button1_Click(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) _
        Handles Button1.Click
            Dim xIndex As Integer = 0
            For Index As Int32 = 0 To (CheckedListBox1.Items.Count - 1)
                xIndex = Index
                If CheckedListBox1.GetItemChecked(Index) Then
                    Dim Item =
                        (
                            From T In dt.AsEnumerable
                            Where T.Field(Of String)("Text") =
                                CheckedListBox1.Items(xIndex).ToString
                            Select T.Field(Of String)("DocumentName")
                        ).First
                    PrintDocument1.DocumentName = Item
                    '
                    ' Do your printing
                    '
                End If
            Next
        End Sub
        Private Function LoadDataFromSomeplace() As DataTable
            Dim dt As New DataTable
            dt.Columns.AddRange(New DataColumn() _
                {New DataColumn With {.ColumnName = "ID", .DataType = GetType(Int32), .AutoIncrement = True, .AutoIncrementSeed = 1, .ReadOnly = True},
                 New DataColumn With {.ColumnName = "Text", .DataType = GetType(String)},
                 New DataColumn With {.ColumnName = "DocumentName", .DataType = GetType(String)}})
            dt.Rows.Add(New Object() {Nothing, "Account 1", "Your info on account 1"})
            dt.Rows.Add(New Object() {Nothing, "Account 2", "Your info on account 2"})
            dt.Rows.Add(New Object() {Nothing, "Account 3", "Your info on account 3"})
            Return dt
        End Function
    End Class

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: print listbox item in multiple pages

    1. "Dim x As Integer" - First of this is an iterator variable used in a For loop, so you can change "For x = 0 To" to "For x As Integer = 0 To". Second, when declaring variables you should initialize them explicitly: "Dim x As Integer = 0" or "Dim x As New Integer".
    2. "yPos = yPos + Font2.Height" can be shortened to "yPos += Font2.Height". In fact this can be done for all arithmetic operations on any variable.
    3. The many references to "e.Graphics" could be wrapped in a "With ... End With" block.
    4. Also look up on string interpolation vs concatenation using the "&" operator: https://docs.microsoft.com/en-us/dot...olated-strings.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width