乱打印的问题
是这样的:一个窗口有好几个打印按钮,启动程序后第一次无论打印那个都正常打印,可是打印另外一个就乱打印,好像是没有清除printdocument,请问怎么清除?
从来没有遇到过。也没见你贴代码。
是自由控制打印机还是使用已封装的类打印的?
从你上次发的贴子看,是不是你写的某个类NEW之后没有销毁引起的?(print之后,dispose下)
程序代码: Private Sub Button38_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button38.Click
Try
PrintDocument1.DefaultPageSettings = printpagesettings
stringtoprint = RichTextBox5.Text
PrintDialog1.Document = PrintDocument1
PageSetupDialog1.PageSettings = printpagesettings
PrintDialog1.PrinterSettings.Copies = 1 ''打印的份数
If PrintDialog1.ShowDialog = DialogResult.OK Then
PageSetupDialog1.PageSettings.Landscape = True
If PageSetupDialog1.ShowDialog = DialogResult.OK Then
AddHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicxcxukezheng
PrintDocument1.Print()
End If
End If
Catch ex As Exception
MessageBox.Show("打印出现错误", ex.ToString())
End Try
End Sub Private Sub printgraphicxcxukezheng(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim x As Integer
Dim y As Integer
x = 995
y = 110
ev.Graphics.DrawString(TextBox44.Text, New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 455, 274)
ev.Graphics.DrawString(TextBox39.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
y = y + 50
ev.Graphics.DrawString(TextBox41.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
y = y + 50
ev.Graphics.DrawString(ComboBox6.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
y = y + 50
ev.Graphics.DrawString(TextBox40.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
y = y + 50
ev.Graphics.DrawString(TextBox38.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
ev.Graphics.DrawString("查询代码", New Font("宋体", 16, FontStyle.Regular), Brushes.Black, 405, 700)
ev.Graphics.DrawString(DateTimePicker4.Text, New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 419, 753)
Dim numchars As Integer
Dim numlines As Integer
Dim stringfopage As String
Dim strformat As New StringFormat
Dim rectdraw As New RectangleF(820, 325, 530, 237)
Dim sizemeasure As New SizeF(543, 180 - printfont.GetHeight(ev.Graphics))
strformat.Trimming = StringTrimming.Word
ev.Graphics.MeasureString(stringtoprint, printfont, sizemeasure, strformat, numchars, numlines)
stringfopage = stringtoprint.Substring(0, numchars)
ev.Graphics.DrawString(stringfopage, printfont, Brushes.Black, rectdraw, strformat)
x = Nothing
y = Nothing
ev.HasMorePages = False
End Sub
程序代码:Imports System.Drawing.Printing
Public Class Form1
Dim stringtoprint As String
Dim printpagesettings As PageSettings
Private Sub Button38_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button38.Click
Try
printpagesettings = New PageSettings'对打印纸张进行设置就是这个对象。记得你上个贴子问过。
'然后,你可以通过printpagesettings对象设置打印的各种属性
PrintDocument1.DefaultPageSettings = printpagesettings
stringtoprint = RichTextBox5.Text
PrintDialog1.Document = PrintDocument1
PageSetupDialog1.PageSettings = printpagesettings
PrintDialog1.PrinterSettings.Copies = 1 ''打印的份数
If PrintDialog1.ShowDialog = DialogResult.OK Then
PageSetupDialog1.PageSettings.Landscape = True
If PageSetupDialog1.ShowDialog = DialogResult.OK Then
AddHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicxcxukezheng
PrintDocument1.Print()
End If
End If
Catch ex As Exception
MessageBox.Show("打印出现错误", ex.ToString())
End Try
End Sub
Private Sub printgraphicxcxukezheng(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim x As Integer
Dim y As Integer
Dim printfont As New Font("宋体", 13, FontStyle.Regular)
x = 995
y = 110
ev.Graphics.DrawString(TextBox44.Text, New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 455, 274)
ev.Graphics.DrawString(TextBox39.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
y = y + 50
ev.Graphics.DrawString(TextBox41.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
y = y + 50
ev.Graphics.DrawString(ComboBox6.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
y = y + 50
ev.Graphics.DrawString(TextBox40.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
y = y + 50
ev.Graphics.DrawString(TextBox38.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
ev.Graphics.DrawString("查询代码", New Font("宋体", 16, FontStyle.Regular), Brushes.Black, 405, 700)
ev.Graphics.DrawString(DateTimePicker4.Text, New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 419, 753)
Dim numchars As Integer
Dim numlines As Integer
Dim stringfopage As String
Dim strformat As New StringFormat
Dim rectdraw As New RectangleF(820, 325, 530, 237)
Dim sizemeasure As New SizeF(543, 180 - printfont.GetHeight(ev.Graphics).ToString)
strformat.Trimming = StringTrimming.Word
ev.Graphics.MeasureString(stringtoprint, printfont, sizemeasure, strformat, numchars, numlines)
stringfopage = stringtoprint.Substring(0, numchars)
ev.Graphics.DrawString(stringfopage, printfont, Brushes.Black, rectdraw, strformat)
x = Nothing
y = Nothing
ev.HasMorePages = False
End Sub
End Class
