如何将文本框发送到打印机

时间:2014-04-14 13:43:17

标签: .net vb.net printing textbox

我在Visual Basic中编写程序,我希望用户可以打印TextBox1.Text的文件

这是我的代码部分:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    PrintDialog1.Document = PrintDocument1
    PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
    PrintDialog1.AllowSomePages = True
    If PrintDialog1.ShowDialog = DialogResult.OK Then
        PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
        PrintDocument1.Print()
    End If
End Sub

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

你实际上并没有打印任何东西。 Have a look at this page。它有一个完整的例子,说明如何进行打印。

基本上您需要将代码放入PrintDocument.PrintPage事件中。

对于Exmaple:

Private Sub PrintDocument_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim linesPerPage As Single = 0
    Dim yPos As Single = 0
    Dim count As Integer = 0
    Dim leftMargin As Single = ev.MarginBounds.Left
    Dim topMargin As Single = ev.MarginBounds.Top
    Dim line As String = Nothing 

    ' Calculate the number of lines per page.
    linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

    ' Print each line of the file. 
    While count < linesPerPage
        line = streamToPrint.ReadLine()
        If line Is Nothing Then 
            Exit While 
        End If
        yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
        ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
        count += 1
    End While 

    ' If more lines exist, print another page. 
    If (line IsNot Nothing) Then
        ev.HasMorePages = True 
    Else
        ev.HasMorePages = False 
    End If 
End Sub