We help IT Professionals succeed at work.

Printing the contents of a TextBox

Mr_Fulano
Mr_Fulano asked
on
Medium Priority
742 Views
Last Modified: 2012-05-11
Hi, I'm using VB.NET2010, WinForms. I have a Form with a textbox, where I append some text to the textbox after parsing an external text file. That part works well.

I allow my users to scroll through the textbox and view the output, but I'd also like to allow them to print the context of the textbox.

How can I accomplish that?

Thanks,
Fulano
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
I gave an example here:  https://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_26901714.html . It prints a ListBox, but you could modify it to print the TextBox's text.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi Kaufmed, your solution almost works...It prints the first line vertically and nothing else. I think the problem is that you were using a listbox and I'm using a Textbox. I'll have to look into it further. I would also like a Print Preview of what is going to be printed rather than launching it to the printer blindly.

Fulano

Author

Commented:
Hi Mike, I'll look you suggestion over, but it rather long. May take me a while.

Thanks,
Fulano
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Carlos VillegasFull Stack .NET Developer

Commented:
kaufmed solution will work if it implement the StringFormat class to wrap the text to the printable area...
Carlos VillegasFull Stack .NET Developer

Commented:
It is a parameter of e.Graphics.DrawString(.....), the printable area is required too, maybe e.PageBounds
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
An example:
Untitled.png

Author

Commented:
I"m working through kaufmed new solution. It prints the first page (albeit with too many spaces between the lines), but it stops at page 1. I'm going to try and make it print the other pages as well.

I am SOOOOOO disgusted with Microsoft that they've sold Visual Studio for so many years and have not addressed the problems with the printing features. This absolutely is the worst for printing that I've every seen.

Fulano

 

Author

Commented:
OK, here is the end result...Its a combination of some of kaufmed's suggestions with the PrintPreview option and the suggestions made by Miketonny. I cobbled them both together to get what I needed. Thanks to you both.

Fulano
Dim StringToPrint As String

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        StringToPrint = tbxOutPut.Text
        Me.PrintPreviewDialog1.Document = Me.PrintDocument1
        Me.PrintPreviewDialog1.ShowDialog()
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim numChars As Integer
        Dim numLines As Integer
        Dim stringForPage As String
        Dim strFormat As New StringFormat()
        Dim PrintFont As New Font("Courier", 7)

        PrintFont = tbxOutPut.Font

        Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
        Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))

        strFormat.Trimming = StringTrimming.Word

        e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)


        stringForPage = StringToPrint.Substring(0, numChars)

        e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw)

        If numChars < StringToPrint.Length Then

            StringToPrint = StringToPrint.Substring(numChars)

            e.HasMorePages = True

        Else

            e.HasMorePages = False

        End If
    End Sub

Open in new window

Author

Commented:
Good suggestions. - Thanks!
Carlos VillegasFull Stack .NET Developer

Commented:
Hello Fulano, I know that it is a closed question but I want to share this, if your print requirements are very basic maybe this can help you too:
    Sub SimpleTextPrint(ByVal text As String, Optional ByVal toDefaultPrinter As Boolean = True)
        Dim myTempFilePath As String = Nothing
        Try
            myTempFilePath = System.IO.Path.GetTempFileName()
            System.IO.File.WriteAllText(myTempFilePath, text)
            Using notepad As New System.Diagnostics.Process()
                notepad.StartInfo = New System.Diagnostics.ProcessStartInfo("notepad", IIf(toDefaultPrinter, "/P ", "") & """" & myTempFilePath & """")
                notepad.StartInfo.WindowStyle = IIf(toDefaultPrinter, ProcessWindowStyle.Hidden, ProcessWindowStyle.Maximized)
                notepad.Start()
                notepad.WaitForExit()
            End Using
        Catch ex As Exception
        Finally
            If Not String.IsNullOrEmpty(myTempFilePath) Then
                System.IO.File.Delete(myTempFilePath)
            End If
        End Try
    End Sub

Open in new window

I still preferring the custom .NET printing, but I just want to share this approach too.
Have a nice day.
Carlos VillegasFull Stack .NET Developer

Commented:
:p I forgot to do some err handling code
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.