-1

i want to print variable value to next page (new page).here's my code

Imports System.Drawing.Printing
Public Class Form1
    Dim ppd As New PrintPreviewDialog


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim custom As New PaperSize("custom", 800, 800)
        ppd.Document = PrintDocument1
        PrintDocument1.DefaultPageSettings.PaperSize = custom
        ppd.ShowDialog()
    End Sub

    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim var_name_s, var_idx_s, var_val_s As String
        Dim var_name, var_idx, var_val, row_idx As Integer
        Dim col1 As Integer = 20
        Dim col2 As Integer = 40
        Dim row1 As Integer = 10
        Static start_var_val As Integer = 1

        For var_name = 1 To 2
            If var_name = 1 Then
                var_name_s = "X"
            Else
                var_name_s = "Y"
            End If

            For var_idx = 1 To 2
                var_idx_s = var_idx.ToString
                e.Graphics.DrawString(var_name_s, New Drawing.Font("Times New Roman", 15), Brushes.Black, col1, row1)
                e.Graphics.DrawString(var_idx_s, New Drawing.Font("Times New Roman", 15), Brushes.Black, col2, row1)
                row_idx += 1
                For var_val = start_var_val To 7
                    var_val_s = var_val.ToString
                    row1 += 20
                    If var_name = 2 Then
                        e.HasMorePages = True
                        row_idx = 0
                    End If
                    If row_idx = 6 Then
                        e.HasMorePages = True
                        start_var_val = var_val + 1
                        row_idx = 0
                        Return
                    End If
                    e.Graphics.DrawString(var_val_s, New Drawing.Font("Times New Roman", 15), Brushes.Black, col1, row1)
                    row_idx += 1

                Next var_val
                row1 += 20
            Next var_idx
        Next var_name

    End Sub
End Class

the condition is

  1. if the page has 6 rows then next row printed on next page
  2. if the var_name changed then the var_name and the var_name's value printed on next page

but the result just never stop print new page and the result like this

'page1
x1
1
2
3
4
5
-------
'other page
x1
7
x2
7
y1
7
y2
7

i want like this

'page1
x1
1
2
3
4
5
---------
'page 2
6
7
x2
1
2
3
---------
'page 3
4
5
6
7


---------
'page 4
y1 'var_name changed
1
2
3
4
5
---------
'page 5
6
7
y2
1
2
3
---------
'page 6
4
5
6
7

---------

can someone help me ? Thanks

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Please elaborate on "not working"? Do you get an error? Is the result just not what you expect? And what is the result? – Jacob M. May 12 '18 at 14:35
  • the program work well, but the result is not what i want, the result never stop to print page, and the page 1 result is x1 1 2 3 4 5, and the other page is x1 7 x2 7 y1 7 y2 7 – Samuel Lawiet May 13 '18 at 01:21
  • 1
    Please don't edit your question to indicate that the problem has been solved. Stack Overflow doesn't work that way. Instead write your solution as an answer and mark it as accepted when time allows. – Visual Vincent May 13 '18 at 09:06

1 Answers1

0

When you set e.HasMorePages to true. the PrintPage event will fire again. You have to track the current page in the class, and only print the information you need for that one page each time the event is raised. Here's a shorter version:

Public Class Form1
    Private ppd As New PrintPreviewDialog
    Private curPage As Integer

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim custom As New PaperSize("custom", 800, 800)
        ppd.Document = PrintDocument1
        PrintDocument1.DefaultPageSettings.PaperSize = custom
        curPage = 1
        ppd.ShowDialog()
    End Sub

    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Select Case curPage
            Case 1
                'Print just page 1
            Case 2
                'Print just page 2
            Case 3
                '...
            Case 4

            Case 5

            Case 6

            Case 7

        End Select
        If curPage <> 7 Then e.HasMorePages = True
        curPage += 1      
    End Sub
End Class

Alternatively, it's more common to have a set of data to work through than to know how many pages you need. In this situation, you may also need to track how far down the page you print, and then remember the finishing position or state of the data set between events.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794