Du ziehst auf deine Form eine PrintDocument-Komponente (PrintDocument1) und eine TextBox (TextBox1).
Dann fügst du unter Projekt-Menü ein Klassenmodul ein, das du vernünftig TextBoxDrucken.vb nennen kannst. Dann fügst du diesen korrigierten Code ein (bestehende Zeilen löschen)
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms
Public Class TextBoxDrucken
Dim PosInStr As Integer
Dim IsPrinting As Boolean
Dim Coll As New List(Of String)
Public Sub Print(ByVal PrintDoc As PrintDocument, ByVal tb As TextBox, _
ByVal ForeColor As Brush, _
ByVal Font As String, ByVal FontSize As Integer, ByVal FontStyle As _
FontStyle, ByVal e As PrintPageEventArgs)
Dim left As Decimal = PrintDoc.DefaultPageSettings.Margins.Left
Dim right As Decimal = PrintDoc.DefaultPageSettings.Margins.Right
Dim top As Decimal = PrintDoc.DefaultPageSettings.Margins.Top
Dim bottom As Decimal = PrintDoc.DefaultPageSettings.Margins.Bottom
Dim pagewidth As Integer = PrintDoc.DefaultPageSettings.Bounds.Width - _
right
Dim pageheight As Integer = PrintDoc.DefaultPageSettings.Bounds.Height _
- bottom
Dim rect As New Rectangle(left, top, 0.0, 0.0)
Dim f As New Font(tb.Font.ToString, tb.Font.Size, tb.Font.Style)
Dim brush As New SolidBrush(tb.ForeColor)
Dim StrIn As String
Dim StrInSize As SizeF
If IsPrinting = False Then
StrIn = tb.Text
For i As Integer = 0 To Len(StrIn)
If i < Len(StrIn) Then
Coll.Add(StrIn.Substring(i, 1))
ElseIf i = Len(StrIn) Then
Coll.Add(StrIn.Substring(i))
End If
Next
IsPrinting = True
End If
If IsPrinting = True Then
For i As Integer = PosInStr To Coll.Count - 1
StrIn = Coll.Item(i).ToString
StrInSize = e.Graphics.MeasureString(StrIn, f, 8, _
StringFormat.GenericTypographic)
rect.Width = StrInSize.Width
e.Graphics.DrawString(StrIn, f, brush, rect.X, rect.Y)
If rect.X + StrInSize.Width < pagewidth Then
rect.X += StrInSize.Width
ElseIf rect.X + StrInSize.Width >= pagewidth Then
rect.X = left
rect.Y += StrInSize.Height
End If
If rect.Y + StrInSize.Height > pageheight Then
PosInStr = i + 1
e.HasMorePages = True
Exit For
ElseIf rect.Y + StrInSize.Height < pageheight Then
e.HasMorePages = False
If i = Coll.Count - 1 Then
IsPrinting = False
Coll.Clear()
PosInStr = 0
End If
End If
Next
End If
End Sub
End Class Dann könnte es gehen mit dem Code für die Form selbst. (Ob der Code auch sonst funktioniert habe ich nicht getestet).
Those people who think they know everything are a great annoyance to those of us who do - Isaac Asimov |