| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4085 人关注过本帖
标题:乱打印的问题
只看楼主 加入收藏
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:41 
乱打印的问题
是这样的:一个窗口有好几个打印按钮,启动程序后第一次无论打印那个都正常打印,可是打印另外一个就乱打印,好像是没有清除printdocument,请问怎么清除?
搜索更多相关主题的帖子: 打印 
2013-05-29 12:06
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
收藏
得分:20 
从来没有遇到过。也没见你贴代码。
是自由控制打印机还是使用已封装的类打印的?
从你上次发的贴子看,是不是你写的某个类NEW之后没有销毁引起的?(print之后,dispose下)

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-29 18:03
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
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
收藏
得分:0 
因为我不能直接测试你的代码,无法重现你的错误。
建议
在PrintDocument1.Print()后加上PrintDocument1.DisPose()
在printgraphicxcxukezheng方法最后一行加上ev.Graphics.DisPose()
再测试下。

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-29 20:06
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
收藏
得分:0 
以下是引用smy727在2013-5-29 19:47:50的发言:

原来是 PrintDocument1.Dispose()呀,呵呵呵呵  哎呀真笨,谢谢你!
不客气。原来在我回帖时,你已解决了问题,祝贺下。

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-29 20:07
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
收藏
得分:0 
按您说的做了,第一次打印没问题,第二次打印其它打印是出现错误
图片附件: 游客没有浏览图片的权限,请 登录注册

下面处出现这个错误:
在 System.ArgumentException 中第一次偶然出现的“System.Drawing.dll”类型的异常
中第一次偶然出现的“System.Drawing.dll”类型的异常
2013-05-30 16:40
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
收藏
得分:0 
不妨把项目传上来,我好重现你的问题。

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-30 17:40
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
收藏
得分:0 
给个邮箱吧
2013-05-30 17:57
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
收藏
得分:0 
还是这样说,你不上传项目,无法能重现或理解你的问题。因为你贴的代码中的部分对象我不知你是如何定义的。
根据你贴的代码,我通过分析你的代码,自已新建了一个项目,作了自已的定义,但没有能够重现你的问题。
仅供你参考
程序代码:
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

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-30 18:59
快速回复:乱打印的问题
数据加载中...
 
   



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

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