标题:乱打印的问题
取消只看楼主
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
结帖率:75%
已结贴  问题点数:20 回复次数:24 
乱打印的问题
是这样的:一个窗口有好几个打印按钮,启动程序后第一次无论打印那个都正常打印,可是打印另外一个就乱打印,好像是没有清除printdocument,请问怎么清除?
搜索更多相关主题的帖子: 打印 
2013-05-29 12:06
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
是这样的:
程序代码:
 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
2013-05-29 19:21
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
原来是 PrintDocument1.Dispose()呀,呵呵呵呵  哎呀真笨,谢谢你!
2013-05-29 19:47
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
按您说的做了,第一次打印没问题,第二次打印其它打印是出现错误

下面处出现这个错误:
在 System.ArgumentException 中第一次偶然出现的“System.Drawing.dll”类型的异常
中第一次偶然出现的“System.Drawing.dll”类型的异常
2013-05-30 16:40
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
给个邮箱吧
2013-05-30 17:57
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
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)
2013-05-30 19:22
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
程序代码:
 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

这是部分代码,我看这足够了。这些代码全部在一个窗口里
2013-05-30 19:28
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
要是设置纸张大小怎么做?
2013-05-30 19:30
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
我是一个窗口里用TabControl1控件有5个TabPage页面,所以代码多、乱
2013-05-30 19:35
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
试过了,还不行,我想问题出在ev.Graphics上,加上ev.Graphics.Dispose()他就出现错误(第二次打印时),不加上就乱打印(第一次打印的和第二次打印的覆盖打印)。真是头痛
2013-05-31 13:49



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-412490-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.073792 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved