You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET VS2012新页面打印变量值问题求助

Fixing Infinite Page Generation & Meeting Print Requirements in VB.NET

Let's break down what's going wrong with your current code and fix it to match your expected output. The main issues are:

  • Static variables (start_var_val) that don't reset properly between print jobs, causing infinite loops
  • Incorrect handling of e.HasMorePages that triggers endless new pages
  • No proper logic to force a new page when var_name changes
  • Messy row counting that doesn't reliably track when to break to a new page

Step 1: Replace Static Variables with Class-Level State Tracking

Instead of using static variables to track progress, we'll use class-level fields to keep track of exactly where we are in the print sequence. This ensures we reset properly every time you click print.

Modified Full Code

Imports System.Drawing.Printing

Public Class Form1
    Dim ppd As New PrintPreviewDialog
    ' Class-level variables to track print progress
    Private currentVarName As Integer = 1 ' Tracks current X/Y (1=X, 2=Y)
    Private currentVarIdx As Integer = 1 ' Tracks current index (1/2)
    Private currentVal As Integer = 1 ' Tracks the next value to print
    Private currentRowCount As Integer = 0 ' Tracks rows printed on current page

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Reset all tracking variables for a new print job
        currentVarName = 1
        currentVarIdx = 1
        currentVal = 1
        currentRowCount = 0

        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 col1 As Integer = 20
        Dim col2 As Integer = 40
        Dim row1 As Integer = 10
        Dim varNameS As String = If(currentVarName = 1, "X", "Y")
        Dim varIdxS As String = currentVarIdx.ToString()

        ' Rule 2: If we're starting a new var_name/var_idx, force a new page (unless it's the first page)
        If currentRowCount > 0 Then
            ' We need to start a new page for the new variable
            e.HasMorePages = True
            currentRowCount = 0
            Return
        End If

        ' Print the variable header (X1/Y1/X2/Y2) if we're starting a new variable on this page
        e.Graphics.DrawString(varNameS, New Drawing.Font("Times New Roman", 15), Brushes.Black, col1, row1)
        e.Graphics.DrawString(varIdxS, New Drawing.Font("Times New Roman", 15), Brushes.Black, col2, row1)
        row1 += 20
        currentRowCount += 1

        ' Print values until we hit 6 rows or run out of values for this variable
        While currentRowCount < 6 AndAlso currentVal <= 7
            ' Print the current value
            e.Graphics.DrawString(currentVal.ToString(), New Drawing.Font("Times New Roman", 15), Brushes.Black, col1, row1)
            row1 += 20
            currentRowCount += 1
            currentVal += 1

            ' If we hit 6 rows, we need a new page
            If currentRowCount = 6 Then
                e.HasMorePages = True
                Return
            End If
        End While

        ' If we've finished all values for current variable index, move to next index or var_name
        If currentVal > 7 Then
            currentVal = 1
            currentVarIdx += 1

            ' If we've finished both indexes for current var_name, move to next var_name
            If currentVarIdx > 2 Then
                currentVarIdx = 1
                currentVarName += 1
            End If
        End If

        ' Check if there's more content to print
        e.HasMorePages = currentVarName <= 2
    End Sub
End Class

Key Fixes & Explanations

  1. Class-Level State Tracking:

    • We use currentVarName, currentVarIdx, currentVal, and currentRowCount to track exactly where we are in the print sequence. These are reset every time you click the print button, so each print job starts fresh.
  2. Var_Name Change Page Break:

    • Before printing a new variable (X→Y or X1→X2), we check if we're already in the middle of a page. If so, we trigger a new page (e.HasMorePages = True) and reset the row count, ensuring the new variable starts on a clean page.
  3. 6-Row Page Limit:

    • We track currentRowCount and stop printing values once it hits 6. We set e.HasMorePages = True and exit the method, so the next page continues from where we left off.
  4. Preventing Infinite Pages:

    • We only set e.HasMorePages = True if there's still content left to print (currentVarName <= 2). Once we've printed both X1, X2, Y1, Y2 and all their values, this becomes false, stopping the page generation.

Expected Output

This code will produce exactly the output you wanted:

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

内容的提问来源于stack exchange,提问作者Samuel Lawiet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:34:46