我忙着在Vb写票务程序,一切都很完美 但是当我打印出来的东西出错时, 票证正确打印(在此示例3中)票证号码重叠 例如:3张门票15200,15201,15203 打印3张票,但1520打印正确,但在evey票上,1,2 a 3打印在彼此之上 到底怎么了?
这是代码的一部分
Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim AZ As String
Dim x As Integer
Dim LB1, LB2 As Integer
AZ = TextBox12.Text
For x = 0 To AZ - 1
LB1 = Val(TextBox1.Text) + x
e.Graphics.DrawString(LB1, New Font("Microsoft Sans Serif", 10), Brushes.Black, New Point(155, 265))
next
end sub
答案 0 :(得分:0)
抱歉,我误读了你的错误答案。
我过去做过的一种方法是在循环中调用printdocument.print。唯一的问题是你得到一个消息框,告诉你每个页面都在打印,这可能不太理想。
无论如何,这是一个你可能能够解决的方法。我已经使用了printdocument,因为我发现ReportViewer / Report更容易使用。
'Widen the scope of LB1 so it can be used in both methods
Dim LB1, LB2 As Integer 'Declared outside of any methods (Class Level)
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(LB1.ToString, New Font("microsoft sans serif", 10), Brushes.Black, New Point(155, 265))
End Sub
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
Dim AZ As Integer = CInt(NumericUpDown1.Value)
For x = 0 To AZ - 1
'We can get rid of this if we use a NumericUpDown control and set it
'to ensure we are working with a number.
If Not Integer.TryParse(TextBox1.Text, LB1) Then
'notify the user with maybe a messagebox that the input is invalid
Exit Sub
End If
LB1 += x
PrintDocument1.Print()
Next
End Sub
同样,我暂时没有使用它,但我似乎记得e.HasMorePages是告诉PrintDocument移动到下一页的一种方式。你可以谷歌上去,看看它是否比循环和显示每个页面的打印消息框更好,如此代码所做的那样。希望它至少可以让你有所作为。
编辑---这是做你想做什么的正确方法---
我决定继续自己研究一下HasMorePages,这将是更好,更合适的解决方案。您需要更改一些内容,但这是使用它的代码示例。
Dim LB1, LB2 As Integer 'Declared outside of any methods
Dim currentTicket = 0
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'how many tickets to print
Dim TotalTickets As Integer = CInt(NumericUpDown1.Value)
'build the current ticket number (I would look at my previous code
'which uses TryParse instead of Val as an invalid input could cause
'your application to fail)
LB1 = Val(TextBox1.Text) + currentTicket
'Print the ticket number on the page
e.Graphics.DrawString(LB1.ToString, New Font("microsoft sans serif", 10), Brushes.Black, New Point(155, 265))
'increase the currentTicket counter
currentTicket += 1
'did we go over the total number of tickets we want to print
If currentTicket < TotalTickets Then
'if not, we need to tell the printdocument that we have more to print
e.HasMorePages = True
End If
'otherwise, we'll stop there and print the pages we produced.
End Sub
这将为您提供单个对话框,显示正在打印的页数而不是每页一个对话框。