[Help]Print Text Box [Solved]

Posts 1–8 of 8 · Page 1 of 1
[Help]Print Text Box [Solved]
Ok how would i do this

i want the info that someone puts in to my textbox. the name of the txt box is txtTextToPrint

and when button1 is clicked it will bring up the PrintDialog1.ShowDialog() then it will print the text in the text box
First Import the namespace:

Code:
Imports System.Drawing.Printing
Then declare variables required for printing
Code:
Dim ps As New PageSettings()
    Dim doc As New PrintDocument()
    Dim pd As New PrintDialog
Put this code on the 'Print' button:

Code:
doc.DefaultPageSettings = ps
        pd.Document = doc
        If Not pd.ShowDialog = Windows.Forms.DialogResult.Cancel Then
            doc.Print()
    End If
Then create a sub that will print the contents of textbox (or whatever string text you want to print...just replace str's content with your's)

Code:
Private Sub doc_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)

        Dim str As String = TextBox1.Text
        Dim chars As Integer
        Dim lines As Integer
        Dim b As New SolidBrush(Color.Blue)
        Dim f As New Font("Arial", 12)
        Dim strf As New StringFormat()
        strf.Trimming = StringTrimming.Word
        Dim rect As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
        Dim sz As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - f.GetHeight(e.Graphics))
        e.Graphics.MeasureString(str, f, sz, strf, chars, lines)
        Dim printstr As String = str.Substring(0, chars)
        e.Graphics.DrawString(printstr, f, b, rect, strf)
        If str.Length > chars Then
            str = str.Substring(chars)
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub
Hope this helps !!
Here is my code, it only prints a blank page. can you help?




Imports System.Drawing.Printing

Public Class Form1

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(txtTextToPrint.Text, New Font("Arial", 40, FontStyle.Regular), Brushes.Black, 200, 200)
PrintDocument1.Print()
End Sub

Private Sub PrintPreviewDialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewDialog1.Load

End Sub

Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs ) Handles MenuStrip1.ItemClicked

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintDialog1.ShowDialog()

Dim ps As New PageSettings()
Dim doc As New PrintDocument()
Dim pd As New PrintDialog

doc.DefaultPageSettings = ps
pd.Document = doc
If Not pd.ShowDialog = Windows.Forms.DialogResult.Cancel Then

End If
doc.Print()

PrintDialog1.ShowDialog()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
PageSetupDialog1.ShowDialog()
End Sub

Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click

End Sub
Private Sub doc_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)

Dim str As String = txtTextToPrint.Text
Dim chars As Integer
Dim lines As Integer
Dim b As New SolidBrush(Color.Blue)
Dim f As New Font("Arial", 12)
Dim strf As New StringFormat()
strf.Trimming = StringTrimming.Word
Dim rect As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
Dim sz As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - f.GetHeight(e.Graphics))
e.Graphics.MeasureString(str, f, sz, strf, chars, lines)
Dim printstr As String = str.Substring(0, chars)
e.Graphics.DrawString(printstr, f, b, rect, strf)
If str.Length > chars Then
str = str.Substring(chars)
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub

End Class
Quote Originally Posted by zmansquared View Post
Here is my code, it only prints a blank page. can you help?




Imports System.Drawing.Printing

Public Class Form1

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(txtTextToPrint.Text, New Font("Arial", 40, FontStyle.Regular), Brushes.Black, 200, 200)
PrintDocument1.Print()
End Sub

Private Sub PrintPreviewDialog1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewDialog1.Load

End Sub

Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs ) Handles MenuStrip1.ItemClicked

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintDialog1.ShowDialog()

Dim ps As New PageSettings()
Dim doc As New PrintDocument()
Dim pd As New PrintDialog

doc.DefaultPageSettings = ps
pd.Document = doc
If Not pd.ShowDialog = Windows.Forms.DialogResult.Cancel Then

End If
doc.Print()

PrintDialog1.ShowDialog()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
PageSetupDialog1.ShowDialog()
End Sub

Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click

End Sub
Private Sub doc_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)

Dim str As String = txtTextToPrint.Text
Dim chars As Integer
Dim lines As Integer
Dim b As New SolidBrush(Color.Blue)
Dim f As New Font("Arial", 12)
Dim strf As New StringFormat()
strf.Trimming = StringTrimming.Word
Dim rect As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
Dim sz As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - f.GetHeight(e.Graphics))
e.Graphics.MeasureString(str, f, sz, strf, chars, lines)
Dim printstr As String = str.Substring(0, chars)
e.Graphics.DrawString(printstr, f, b, rect, strf)
If str.Length > chars Then
str = str.Substring(chars)
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub

End Class
Double click Form1 and replace the existing code with this code:

Code:
Imports System.Drawing.Printing
Public Class Form1
Dim ps As New PageSettings()
    Dim doc As New PrintDocument()
    Dim pd As New PrintDialog

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
doc.DefaultPageSettings = ps
        pd.Document = doc
        If Not pd.ShowDialog = Windows.Forms.DialogResult.Cancel Then
            doc.Print()
    End If
End Sub
Private Sub doc_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)

        Dim str As String = txtTextToPrint.Text
        Dim chars As Integer
        Dim lines As Integer
        Dim b As New SolidBrush(Color.Blue)
        Dim f As New Font("Arial", 12)
        Dim strf As New StringFormat()
        strf.Trimming = StringTrimming.Word
        Dim rect As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
        Dim sz As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - f.GetHeight(e.Graphics))
        e.Graphics.MeasureString(str, f, sz, strf, chars, lines)
        Dim printstr As String = str.Substring(0, chars)
        e.Graphics.DrawString(printstr, f, b, rect, strf)
        If str.Length > chars Then
            str = str.Substring(chars)
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub
End Class
This will work !!!
Meh, No offense MJ but the code you provided is standard MSDN / C&P and not even custom to his problem, Why wouldn't you use the PrintDocument component

So how about a few lines of code, and easy to remember , add PrinterDocument component to your form

Button Click Event

[php]

PrintDocument1.PrinterSettings.Copies = 1
PrintDocument1.Print()
[/php]

Then Add this

[php]

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

e.Graphics.DrawString(TextBox1.Text, TextBox1.Font, Brushes.Blue, 100, 100)

End Sub
[/php]


There you go

Everyone out there is always looking for a solution, and everyone thinks it is so hard, so they C&P and change a few lines of code, why would you even need half of that code?

nvm... Feel like ranting

Simplicity, working and effective.


hope this works


Dude, your so good. Ok let see if you know this one. :P

A place where the user can enter in the font size and change the text color


All you need to do is create 3 combo box's and have them have a colleciton

[php]
1
2
3
4
5
6
7
8
9
10
[/php]

A label above that that says Font Size

and so forth

Keep in mind the Print method is printing the current textbox settings so you can use (pre print)

TextBox1.Font = Combobox1.selectedvalue ' or item
TextBox1.Size = Combobox2.selecteditem '(or value)
Textbox1.color = you get the idea
But where does the textbox code go? textbox or where. I am lost, can you string the code together for me?
Posts 1–8 of 8 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us