-
Aug 17th, 2020, 03:33 PM
#1
Thread Starter
Member
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
-
Aug 17th, 2020, 08:41 PM
#2
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:
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
-
Aug 17th, 2020, 08:59 PM
#3
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:
Private Const ITEMS_PER_PAGE As Integer = 5
Private currentItemIndex As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
'Start at the first item for the new print run.
currentItemIndex = 0
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim x = 100
Dim y = 100
'Assume that there are more pages until we find that there are not.
e.HasMorePages = True
'Only print the specified number of items on the current page.
For i = 1 To ITEMS_PER_PAGE
'Print the current item.
e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items(currentItemIndex)), Font, Brushes.Black, x, y)
'Move to the next item.
currentItemIndex += 1
If currentItemIndex = ListBox1.Items.Count Then
'We have gone past the last item so we are done.
e.HasMorePages = False
Exit For
End If
y += 50
Next
End Sub
-
Aug 18th, 2020, 04:50 AM
#4
Lively Member
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
-
Aug 18th, 2020, 05:27 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|