如何使用 Vb.net 从文本框中打印文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40499985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print a text from textbox using Vb.net?
提问by Mikelemuel
After the transaction, I want all my text from textbox to automatically print. But, my code isn't working. Please someone help me to find a solution or any other way to print the text using any printer
交易后,我希望文本框中的所有文本自动打印。但是,我的代码不起作用。请有人帮我找到解决方案或使用任何打印机打印文本的任何其他方式
Input : TextBox4.Text = "asdf" Output : asdf (on a printed page)
输入:TextBox4.Text = "asdf" 输出:asdf(在打印页面上)
Code
代码
RequiredFieldsGovPriv()
Printer.Print(TextBox3.Text)
Printer.EndDoc
回答by HaPhan
With the limitation of a comment, I can't post the code so here's the whole code to call the printer:
由于评论的限制,我无法发布代码,因此这是调用打印机的完整代码:
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
And then, ...
进而, ...
'working code
RequiredFieldsGovPriv() 'your sub
Dim mprinter As New myPrinter
mprinter.print(txttextbox.Text)
'Continue to work...
These code worked fine within my project. If you still cannot print, post your error message or a screenshot of whatever your program doing when you call the print function.
这些代码在我的项目中运行良好。如果您仍然无法打印,请发布您的错误消息或您的程序在调用打印功能时所做的任何操作的屏幕截图。

