Hi All,

I hope you all would be fine i want to print

1. Only text

2. No controls should be appear in the print like textboxes etc.

Thanks in advance.

Recommended Answers

All 17 Replies

Assuming that your text is in a RichTextBox control.
Try this:

Public Class Form1
   Private WithEvents prn As Printing.PrintDocument

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Form1.Load
   End Sub

   Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
      prn.Print()
   End Sub

   Private Sub prn_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles prn.PrintPage
      Dim linesPerPage As Single = 0
      Dim yPosition As Single = 0
      Dim count As Integer = 0
      Dim leftMargin As Single = e.MarginBounds.Left
      Dim topMargin As Single = e.MarginBounds.Top
      Dim printFont As Font = Me.Font
      Dim myBrush As New SolidBrush(Color.Black)
      Dim text() As String = RichTextBox1.Lines

      linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)

      Try
         For i As Integer = 0 To text.Length - 1
            If count < linesPerPage Then
               yPosition = topMargin + (count * printFont.GetHeight(e.Graphics))
               e.Graphics.DrawString(text(i), printFont, myBrush, leftMargin, yPosition, New StringFormat())
               count += 1
            Else
               Exit For
            End If
         Next
      Catch ex As Exception
      End Try
      'This is needed
      e.HasMorePages = True
      e.HasMorePages = False
      myBrushes = Nothing
   End Sub
End Class

i have to print the text from text boxes not from richtextbox this code give me error that myBrushes is not declared?

Ok.
Then change the line: Dim text() As String = RichTextBox1.Lines into this:

Dim text() As String = New String() {Textbox1.Text, Textbox2.Text, and so on}

And change myBrushes to myBrush.

it gives me this error

Object reference not set to an instance of an object.

What should i do now???

Could you be more specific, "it" is not very descriptive?
Which line gives the error? And what object?

Perhaps if you try changing the line Private WithEvents prn As Printing.PrintDocument into Private WithEvents prn As New Printing.PrintDocument

ok the problem was withevents prn as new printing.document but it does not print any thing page comes black from printer.

Ok.
Then let's try this.
Replace the e.Graphics.DrawString..... with
e.Graphics.DrawString(line, Me.Font, Brushes.Black, leftMargin, yPosition)

Now it is working but

"It is ptrinting the contents of textboxes with textboxes but i want to print only contents without textboxes"

Please help me thanks in advance.

I don't understand what you mean by that.
Does the text lines have borders or something?

I want to just print the text present in textboxes.

but the code given by you prints both text present in trexboxes and also the textboxes.

Is this the code you posted in the thread "Print Problem"?
And are the textboxes printed after the text?

yes the texboxes printed after the text but i just want to print text and also there is an error in this line

e.Graphics.DrawString(line, Me.Font, Brushes.Black, leftMargin, yPosition)

error is line statement is no longer supported I/O........

Please help

It would be helpful with the complete error message.

Also, I just tested the code you posted.
And I experienced no problems what so ever with it.
The text in all the textboxes printed out just find, one line after another, without the actual textboxes.

Dear it would be much better if you send me a form having the coding example of printing whichi needed i would be very thankfull to you.

Thyanks in advance.

I m waiting for your kind reply once agin thaks for your great help.

Thank you so much dear you are realy very great its working thanks alot.

form width is '352' and form height is '498' and i want to print take print on right side how can i do this. please help me thanks in advance.

I have no idea of how to accomplish that.
If you need to print a part of a form, perhaps you should have a look into the PrintForm component in Visual Basic Power Packs 3.0.

Beyond that, I would appreciate it if you would mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.