如何使用Vb.net从文本框中打印文本?

时间:2016-11-09 03:54:12

标签: vb.net

交易完成后,我希望文本框中的所有文本都能自动打印。 但是,我的代码不起作用。请有人帮我找到解决方案或任何其他方式使用任何打印机打印文本

输入:TextBox4.Text =" asdf" 输出:asdf(在打印页面上)

代码

RequiredFieldsGovPriv()    
Printer.Print(TextBox3.Text)
Printer.EndDoc

1 个答案:

答案 0 :(得分:0)

由于评论的限制,我无法发布代码,所以这里是调用打印机的整个代码:

Public Class myPrinter
    Friend TextToBePrinted As String
    Public Sub print(ByVal text As String)
        TextToBePrinted = text
        Dim prn As New Printing.PrintDocument
        Using (prn)
            prn.PrinterSettings.PrinterName = PrinterSettings.InstalledPrinters.Item(0)
            AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler
            prn.Print()
            RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler
        End Using
    End Sub

    Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)
        Dim myFont As New Font("Microsoft San Serif", 10)
        args.Graphics.DrawString(TextToBePrinted, New Font(myFont, FontStyle.Regular), Brushes.Black, 50, 50)
    End Sub
End Class

然后,......

'working code 
RequiredFieldsGovPriv() 'your sub
Dim mprinter As New myPrinter
mprinter.print(txttextbox.Text)
'Continue to work...

这些代码在我的项目中运行良好。如果仍然无法打印,请在调用打印功能时发布错误消息或程序正在执行的屏幕截图。