注册 登录
编程论坛 VB.NET论坛

乱打印的问题

smy727 发布于 2013-05-29 12:06, 4085 次点击
是这样的:一个窗口有好几个打印按钮,启动程序后第一次无论打印那个都正常打印,可是打印另外一个就乱打印,好像是没有清除printdocument,请问怎么清除?
41 回复
#2
不说也罢2013-05-29 18:03
从来没有遇到过。也没见你贴代码。
是自由控制打印机还是使用已封装的类打印的?
从你上次发的贴子看,是不是你写的某个类NEW之后没有销毁引起的?(print之后,dispose下)
#3
smy7272013-05-29 19:21
是这样的:
程序代码:
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

有好几个这样的打印代码,要不要这样?
        PrintDocument1 = Nothing
        PrintDialog1 = Nothing
        PageSetupDialog1 = Nothing
#4
smy7272013-05-29 19:47
原来是 PrintDocument1.Dispose()呀,呵呵呵呵  哎呀真笨,谢谢你!
#5
不说也罢2013-05-29 20:06
因为我不能直接测试你的代码,无法重现你的错误。
建议
在PrintDocument1.Print()后加上PrintDocument1.DisPose()
在printgraphicxcxukezheng方法最后一行加上ev.Graphics.DisPose()
再测试下。
#6
不说也罢2013-05-29 20:07
以下是引用smy727在2013-5-29 19:47:50的发言:

原来是 PrintDocument1.Dispose()呀,呵呵呵呵  哎呀真笨,谢谢你!
不客气。原来在我回帖时,你已解决了问题,祝贺下。
#7
smy7272013-05-30 16:40
按您说的做了,第一次打印没问题,第二次打印其它打印是出现错误
只有本站会员才能查看附件,请 登录

下面处出现这个错误:
在 System.ArgumentException 中第一次偶然出现的“System.Drawing.dll”类型的异常
中第一次偶然出现的“System.Drawing.dll”类型的异常
#8
不说也罢2013-05-30 17:40
不妨把项目传上来,我好重现你的问题。
#9
smy7272013-05-30 17:57
给个邮箱吧
#10
不说也罢2013-05-30 18:59
还是这样说,你不上传项目,无法能重现或理解你的问题。因为你贴的代码中的部分对象我不知你是如何定义的。
根据你贴的代码,我通过分析你的代码,自已新建了一个项目,作了自已的定义,但没有能够重现你的问题。
仅供你参考
程序代码:
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
#11
smy7272013-05-30 19:22
Imports System.Drawing.Printing

Private printpagesettings As New PageSettings
    Private stringtoprint As String
    Private printfont As New Font("宋体", 11)
    Private printfont1 As New Font("宋体", 10, FontStyle.Regular)
#12
smy7272013-05-30 19:28
程序代码:
Private Sub printgraphictongzhidan(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim dfk As New Pen(Color.Black, 2)
        Dim newpen As New Pen(Color.Black)
        ev.Graphics.DrawString("阿 图 什 市 建 设 局 建 设 工 程 申 请 表", New Font("宋体", 18, FontStyle.Bold), Brushes.Black, 177, 99)
        ev.Graphics.DrawRectangle(dfk, 60, 140, 710, 958)
        Dim x1 As Integer
        Dim y1 As Integer
        Dim x2 As Integer
        Dim y2 As Integer
        x1 = 60
        y1 = 180
        x2 = 770
        y2 = 180
        ev.Graphics.DrawLine(newpen, 60, 180, 770, 180)
        y1 = y1 + 40
        y2 = y2 + 40
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 40
        y2 = y2 + 40
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 40
        y2 = y2 + 40
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 239
        y2 = y2 + 239
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 239
        y2 = y2 + 239
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 240
        y2 = y2 + 240
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 80
        y2 = y2 + 80
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
     

        ev.Graphics.DrawLine(newpen, 136, 140, 136, 300)
        ev.Graphics.DrawLine(newpen, 465, 140, 465, 300)
        ev.Graphics.DrawLine(newpen, 541, 140, 541, 300)

        ev.Graphics.DrawString("建设单位", printfont1, Brushes.Black, 71, 151)
        ev.Graphics.DrawString("建设项目", printfont1, Brushes.Black, 71, 192)
        ev.Graphics.DrawString("建筑面积", printfont1, Brushes.Black, 71, 232)
        ev.Graphics.DrawString("总 投 资", printfont1, Brushes.Black, 71, 271)
        ev.Graphics.DrawString("市住建局(城市配套费):", printfont1, Brushes.Black, 71, 314)
        ev.Graphics.DrawString("市统筹站(劳保统筹费):", printfont1, Brushes.Black, 71, 553)
        ev.Graphics.DrawString("市规划局(定桩费):", printfont1, Brushes.Black, 71, 792)
        ev.Graphics.DrawString("主管领导审查结果:", printfont1, Brushes.Black, 71, 1032)


        ev.Graphics.DrawString(建设单位TextBox2.Text, printfont1, Brushes.Black, 140, 152)
        ev.Graphics.DrawString(J新建TextBox.Text, printfont1, Brushes.Black, 140, 192)
        ev.Graphics.DrawString(面积TextBox.Text & 面积标注ComboBox2.Text, printfont1, Brushes.Black, 140, 232)
        ev.Graphics.DrawString(TextBox45.Text, printfont1, Brushes.Black, 140, 271)

        ev.Graphics.DrawString("建设位置", printfont1, Brushes.Black, 474, 151)
        ev.Graphics.DrawString("结    构", printfont1, Brushes.Black, 474, 192)
        ev.Graphics.DrawString("层    数", printfont1, Brushes.Black, 474, 232)
        ev.Graphics.DrawString("定 桩 费", printfont1, Brushes.Black, 474, 271)

        ev.Graphics.DrawString(建设位置ComboBox1.Text, printfont1, Brushes.Black, 546, 152)
        ev.Graphics.DrawString(J结构ComboBox.Text, printfont1, Brushes.Black, 546, 192)
        ev.Graphics.DrawString(层数TextBox1.Text, printfont1, Brushes.Black, 546, 232)
        ev.Graphics.DrawString(TextBox46.Text, printfont1, Brushes.Black, 546, 271)

        x1 = Nothing
        y1 = Nothing
        x2 = Nothing
        y2 = Nothing
        ev.Graphics.Dispose()
        ev.HasMorePages = False

    End Sub
    Private Sub printgraphicshenpi(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim dfk As New Pen(Color.Black, 2)
        Dim newpen As New Pen(Color.Black)
        ev.Graphics.DrawString("阿图什市规划局建设项目选址意见审批表", New Font("宋体", 18, FontStyle.Bold), Brushes.Black, 177, 99)
        ev.Graphics.DrawRectangle(dfk, 60, 140, 710, 958)
        Dim x1 As Integer
        Dim y1 As Integer
        Dim x2 As Integer
        Dim y2 As Integer
        x1 = 60
        y1 = 180
        x2 = 770
        y2 = 180
        ev.Graphics.DrawLine(newpen, 60, 180, 770, 180)
        y1 = y1 + 40
        y2 = y2 + 40
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 146
        y2 = y2 + 146
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 146
        y2 = y2 + 146
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 146
        y2 = y2 + 146
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 146
        y2 = y2 + 146
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 148
        y2 = y2 + 148
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)

        ev.Graphics.DrawLine(newpen, 136, 140, 136, 220)
        ev.Graphics.DrawLine(newpen, 465, 140, 462, 220)
        ev.Graphics.DrawLine(newpen, 541, 140, 541, 220)

     
        ev.Graphics.DrawString("申请单位", New Font("宋体", 10, FontStyle.Bold), Brushes.Black, 70, 152)
        ev.Graphics.DrawString("建设项目", New Font("宋体", 10, FontStyle.Bold), Brushes.Black, 70, 191)
        ev.Graphics.DrawString("建设位置", New Font("宋体", 10, FontStyle.Bold), Brushes.Black, 476, 152)
        ev.Graphics.DrawString("建筑面积", New Font("宋体", 10, FontStyle.Bold), Brushes.Black, 476, 191)
        ev.Graphics.DrawString(TextBox30.Text, printfont1, Brushes.Black, 141, 152)
        ev.Graphics.DrawString(TextBox49.Text, printfont1, Brushes.Black, 141, 191)
        ev.Graphics.DrawString(ComboBox7.Text, printfont1, Brushes.Black, 547, 152)
        ev.Graphics.DrawString(TextBox51.Text, printfont1, Brushes.Black, 547, 191)
        ev.Graphics.DrawString("经办人意见:", printfont1, Brushes.Black, 66, 225)
        ev.Graphics.DrawString("经办人:" & TextBox48.Text & "    日期:" & DateTimePicker5.Text, printfont1, Brushes.Black, 480, 349)
        ev.Graphics.DrawString("签名:              " & "日期:", printfont1, Brushes.Black, 476, 495)
        ev.Graphics.DrawString("签名:              " & "日期:", printfont1, Brushes.Black, 476, 641)
        ev.Graphics.DrawString("签名:              " & "日期:", printfont1, Brushes.Black, 476, 787)
        ev.Graphics.DrawString("签名:              " & "日期:", printfont1, Brushes.Black, 476, 933)
        ev.Graphics.DrawString("签名:              " & "日期:", printfont1, Brushes.Black, 476, 1081)


        Dim numchars As Integer
        Dim numlines As Integer
        Dim stringfopage As String
        Dim strformat As New StringFormat
        Dim rectdraw As New RectangleF(70, 246, 690, 130)
        Dim sizemeasure As New SizeF(690, 130 - 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)
        x1 = Nothing
        y1 = Nothing
        x2 = Nothing
        y2 = Nothing
        ev.Graphics.Dispose()
        ev.HasMorePages = False

    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.Graphics.Dispose()
        ev.HasMorePages = False

    End Sub
    Private Sub printgraphicYDxukezheng(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim x As Integer
        Dim y As Integer
        x = 990
        y = 120
        ev.Graphics.DrawString(TextBox12.Text, New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 450, 274)
        ev.Graphics.DrawString(建设单位TextBox.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString(D新划TextBox.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString(D位置在TextBox.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString(D该用地TextBox.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString(D总面积TextBox.Text & 用地面积标注ComboBox1.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString(TextBox6.Text & 建设规模标注ComboBox.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        x = x - 35
        ev.Graphics.DrawString("建设用地蓝线图:" & 建设用地红线图TextBox.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(DateTimePicker1.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, 420, 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.Graphics.Dispose()
        ev.HasMorePages = False

    End Sub


程序代码:
    Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click
        Try

            PrintDocument1.DefaultPageSettings = printpagesettings
            stringtoprint = RichTextBox4.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.printgraphicYDxukezheng

                    PrintDocument1.Print()
                    PrintDocument1.Dispose()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("打印出现错误", ex.ToString())
        End Try

    End Sub

程序代码:
    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()
                    PrintDocument1.Dispose()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("打印出现错误", ex.ToString())
        End Try

    End Sub

程序代码:
  Private Sub Button42_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button42.Click
        Try

            PrintDocument1.DefaultPageSettings = printpagesettings
            stringtoprint = RichTextBox6.Text
            PrintDialog1.Document = PrintDocument1
            PageSetupDialog1.PageSettings = printpagesettings
            PrintDialog1.PrinterSettings.Copies = 1 ''打印的份数
            If PrintDialog1.ShowDialog = DialogResult.OK Then
                PageSetupDialog1.PageSettings.Landscape = False
                If PageSetupDialog1.ShowDialog = DialogResult.OK Then
                    AddHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicshenpi

                    PrintDocument1.Print()
                    PrintDocument1.Dispose()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("打印出现错误", ex.ToString())
        End Try

      
    End Sub

程序代码:
Private Sub Button43_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button43.Click
        Try

            PrintDocument1.DefaultPageSettings = printpagesettings

            PrintDialog1.Document = PrintDocument1
            PageSetupDialog1.PageSettings = printpagesettings
            PrintDialog1.PrinterSettings.Copies = 1 ''打印的份数
            PageSetupDialog1.PageSettings.Landscape = False
            If PrintDialog1.ShowDialog = DialogResult.OK Then
                If PageSetupDialog1.ShowDialog = DialogResult.OK Then
                    AddHandler PrintDocument1.PrintPage, AddressOf Me.printgraphictongzhidan
                    PrintDocument1.Print()
                    PrintDocument1.Dispose()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("打印出现错误", ex.ToString())
        End Try

    End Sub

这是部分代码,我看这足够了。这些代码全部在一个窗口里
#13
smy7272013-05-30 19:30
要是设置纸张大小怎么做?
#14
smy7272013-05-30 19:35
我是一个窗口里用TabControl1控件有5个TabPage页面,所以代码多、乱
#15
不说也罢2013-05-31 13:16
楼主所说的打印乱,是不是指打印出来的结果布局上零乱?
Private printpagesettings As New PageSettings'这个对象可以定义为窗体公共变量,但不要New
然后在每一个Button的Click事件中写上:printpagesettings = New PageSettings'在10楼给过你例子
在New之后,可以对printpagesettings对象的左右边距等进行设置,
例如:
printpagesettings = New PageSettings '对打印纸张进行设置就是这个对象。记得你上个贴子问过。
'然后,你可以通过printpagesettings对象设置打印的各种属性
printpagesettings.Margins.Left = 50 '以百分之一英寸为单位,设置页面的左边距
PrintDocument1.DefaultPageSettings = printpagesettings
stringtoprint = RichTextBox5.Text


#16
smy7272013-05-31 13:49
试过了,还不行,我想问题出在ev.Graphics上,加上ev.Graphics.Dispose()他就出现错误(第二次打印时),不加上就乱打印(第一次打印的和第二次打印的覆盖打印)。真是头痛
#17
不说也罢2013-05-31 14:26
仔细研究代码的逻辑关系。一步一步来。测试时先把try catch注释掉,查明错误的原因。
#18
smy7272013-05-31 20:25
试过一切办法了问题还是存在,我看就是ev.Graphics解放引起的问题,也说不定是多线程问题,不知道怎么解决,还是求高人指点。谢谢!
#19
不说也罢2013-05-31 20:28
你指的乱是AddHandler调用自定义函数出现混乱,还是在打印输出后,页面布局混乱?
我感觉应当是后者吧?
#20
不说也罢2013-05-31 20:31
TRY模块注释掉后,每出现错误都会抛出一个错误提示及纠正意见的哦。错误出现在哪一行?
任何人想帮到你,必须能重现你的错误才能共同研究解决办法的
#21
不说也罢2013-05-31 20:34
另外,如果报表的功能并不是很复杂,就用.NET自带的datareport吧,它与VB6下的报表有质的不同.也很好用的。设计好报表布局后(所见即所得),只需给报表添加数据源即可。
#22
smy7272013-05-31 20:48
两者都乱,纸张也错,第一次打印的数据在第二次打印页面还有,错误提示如下:
只有本站会员才能查看附件,请 登录

下面的即时窗口显示的“在 System.ArgumentException 中第一次偶然出现的“System.Drawing.dll”类型的异常”
#23
smy7272013-05-31 20:51
程序里也有datareport打印,你说过datareport缓冲慢,总共有8个打印按钮
#24
smy7272013-05-31 20:55
我就不明白ev.Graphics.Dispose()为什么第一次打印没问题第二次就出现错误那?
#25
不说也罢2013-05-31 21:10
你把所有的ev.Graphics.Dispose()替换成ev.Graphics.Clear(Color.White)再试试
#26
smy7272013-05-31 21:27
试了,全是空白纸
#27
smy7272013-05-31 21:32
ev.Graphics.Clear(Color.White)这是清楚颜色呀?
#28
不说也罢2013-05-31 21:40
稍等,我给你一个测试工程
#29
不说也罢2013-05-31 21:58
这个工程是一个新建的项目,在窗体上拉上一个TabControl,在tabpage1上放一个按钮,一个richtextbox,在tabpage2再放上放一个按钮,一个richtextbox,
当然你可以
这应当能够模拟你的项目了,好像。
将下面的代码复制到代码编辑器中
程序代码:
Imports System.Drawing.Printing
Public Class Form1
    Private printpagesettings As PageSettings
    Private stringtoprint As String
    Private printfont As New Font("宋体", 11)
    Private printfont1 As New Font("宋体", 10, FontStyle.Regular)
    Private Sub PrintDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
        PrintDocument1.Dispose()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Try
        printpagesettings = New PageSettings
        PrintDocument1.DefaultPageSettings = printpagesettings
        stringtoprint = RichTextBox1.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.printgraphicYDxukezheng
                PrintDocument1.Print()
                PrintDocument1.Dispose()
                RemoveHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicYDxukezheng
            End If
        End If
        'Catch ex As Exception
        '    MessageBox.Show("打印出现错误", ex.ToString())
        'End Try

    End Sub
    Private Sub printgraphicYDxukezheng(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim x As Integer
        Dim y As Integer
        x = 990
        y = 120
        ev.Graphics.DrawString("RichTextBox1.Text", New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 450, 274)
        ev.Graphics.DrawString("建设单位TextBox.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString("D新划TextBox.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString("D位置在TextBox.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString("D该用地TextBox.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString("D总面积TextBox.Text" & "用地面积标注ComboBox1.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString("TextBox6.Text & 建设规模标注ComboBox.Text", New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        x = x - 35
        ev.Graphics.DrawString("建设用地蓝线图:" & "建设用地红线图TextBox.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("DateTimePicker1.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, 420, 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
        ev.Graphics.Dispose()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Try

        printpagesettings = New PageSettings
        PrintDocument1.DefaultPageSettings = printpagesettings
        stringtoprint = RichTextBox2.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()
                PrintDocument1.Dispose()
                RemoveHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicxcxukezheng
            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("RichTextBox2.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

End Class
我测试了多次,而且注释掉了所有的TRY,一点儿没有重叠的地方。
现在我可以作出判断,如果你经过比对我这个项目代码之后,你仍然出现重叠,那么只有下面的一种可能:
就是你每次ev.Graphics.DrawString的时候,传递过去的string参数的内容过长,而你在之所赋值的y值过小,才会导致重叠现象的发生!


#30
smy7272013-05-31 22:06
这行是什么意思?
  Private Sub PrintDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
        PrintDocument1.Dispose()
    End Sub
#31
smy7272013-05-31 22:13
知道了,应该是打印作业完毕后清楚
#32
smy7272013-05-31 22:32
测试过了,还是不行。慢慢折腾吧,谢谢你!
#33
smy7272013-06-01 19:46
回复 15楼 不说也罢
printdokoa = New PrintDocument
            printdiala = New PrintDialog
            printpagesettings = New PageSettings

            printdokoa.DefaultPageSettings = printpagesettings
            stringtoprint = RichTextBox1.Text
            printdiala.Document = printdokoa
            PageSetupDialog1.PageSettings.PaperSize.Kind = PaperKind.A3

这个代码为什么提示kind为readonly?属性为只读呀?
#34
不说也罢2013-06-02 08:46
以下是引用smy727在2013-6-1 19:46:52的发言:

 printdokoa = New PrintDocument
            printdiala = New PrintDialog
            printpagesettings = New PageSettings

            printdokoa.DefaultPageSettings = printpagesettings
            stringtoprint = RichTextBox1.Text
            printdiala.Document = printdokoa
            PageSetupDialog1.PageSettings.PaperSize.Kind = PaperKind.A3

这个代码为什么提示kind为readonly?属性为只读呀?

kind属性为只读,不可以象你这样直接设置。如果要改变打印纸张大小,应当这样:
        Dim myPaperSize = New System.Drawing.Printing.PaperSize("A3", 1169, 1654)
        PageSetupDialog1.PageSettings.PaperSize = myPaperSize

另外,我在29楼给你的项目,你测试后仍会出现你提出的现象吗?不会吧?——我测试过。
如果这测试项目没有重现你的现象,不用再折腾,分析测试项目的思路,逐渐纠正你的代码吧
#35
smy7272013-06-02 23:26
那请问,A4、B4纸是多少像素?像素是怎么计算的?
#36
smy7272013-06-02 23:53
按您说的做了,打印是正确,可纸张名称为什么是信纸?
#37
不说也罢2013-06-03 12:39
29楼的代码不是有纸张设置的对话框么?可以设置纸张类型,左右上下页边距等?你没测试吗
#38
smy7272013-06-03 12:56
我是想直接设置,不显示页面设置对话框
#39
不说也罢2013-06-03 13:20
object=System.Drawing.Printing.PaperSize()'无参数,表明使用打印机的默认纸张类型,一般为A4纸
object=System.Drawing.Printing.PaperSize(name as string,width as integer,height as integer)'三个参数,一个不可少,第二、三个参数分别表示自定义纸张的宽度和高度,以百分之一英寸为单位,你可以百度下进行换算。例如,A4纸宽度为21CM,高度为29.7CM
则 New System.Drawing.Printing.PaperSize("A4", 827, 1169)

[ 本帖最后由 不说也罢 于 2013-6-3 13:23 编辑 ]
#40
不说也罢2013-06-03 13:49
如果自定义打印,那么,pagesetupdialog控件就没有用处了,该写成下面这样的:
程序代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            printpagesettings = New PageSettings
            printpagesettings.PaperSize = New System.Drawing.Printing.PaperSize("自定义大小", 827, 1169)
            printpagesettings.Landscape = True
            printpagesettings.Margins.Left = 1 '以百分之一英寸为单位,这是设置打印的左边距
            '.....右边距,上、下
            PrintDocument1.DefaultPageSettings = printpagesettings
            stringtoprint = RichTextBox2.Text
            PrintDialog1.Document = PrintDocument1
            PrintDialog1.PrinterSettings.Copies = 1 ''打印的份数
            If PrintDialog1.ShowDialog = DialogResult.OK Then
                AddHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicYDxukezheng '打印的精准度就靠你在这个过程里设置了
                PrintDocument1.Print()
                PrintDocument1.Dispose()
                RemoveHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicYDxukezheng
            End If
        Catch ex As Exception
            MessageBox.Show("打印出现错误", ex.ToString())
        End Try

    End Sub
  
#41
smy7272013-06-04 11:03
RemoveHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicYDxukezheng这一行是什么意思呀?
#42
smy7272013-06-06 00:27
非常感谢
1