ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

How to Print the Text in a TextBox Using Visual Basic 2008

In this tutorial, I’m going to teach you how to print the text in a Textbox using Visual Basic 2008. This will allow you to preview and print any message that you type in a TextBox. It depends on you, on how long your message will be because it will automatically set on the next page.

Lets get started :

Open Visual Basic 2008, create a new Window Application and do the Form just like this. Add the PrintDocument and PrintPreviewDialog.

firstform_1

Now, double click the PrintDocument and do this following code for setting up the font,margin and the process where in the message in the Textbox will appear in the PrintDocument.

  1. Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
  2.         'DECLARE A STATIC VARIABLE
  3.         'HOLD THE POSITION OF THE LAST PRINTED CHARACTER
  4.         'AND IT CAN REFERECE THE SUBSEQUET PRINTPAGE EVENTS.
  5.         Static CurCharInt As Int32
  6.         'SET THE FONT THAT YOU WANT TO USE FOR PRINTING.
  7.         Dim fnt As New Font("Segoe UI", 20)
  8.         Dim printheightInt, printwidthInt, leftmargin, topmargin As Int32
  9.         With PrintDocument1.DefaultPageSettings
  10.             ' SET THE VARIABLE THAT HOLD THE LIMITS OF THE PRINTING AREA RECTANGLE
  11.             printheightInt = .PaperSize.Height - .Margins.Top - .Margins.Bottom
  12.             printwidthInt = .PaperSize.Width - .Margins.Left - .Margins.Right
  13.  
  14.             ' SET THE VARIABLES TO HOLD THE VALUES OF THE MARGIN THAT WILL WORK FOR
  15.             'THE X AND Y COORDINATES OF THE UPPER LEFT CORNER OF THE PRINTING AREA RECTANGLE
  16.             leftmargin = .Margins.Left ' X coordinate
  17.             topmargin = .Margins.Top ' Y coordinate
  18.         End With
  19.  
  20.         ' COMPUTE THE TOTAL LINES IN THE DOCUMENT BASED ON THE HEIGHT OF THE FONT AND ITS PRINTING AREA.
  21.         Dim linecountInt As Int32 = CInt(printheightInt / fnt.Height)
  22.  
  23.         ' SET THE RECTANGLE STRUCTURE THAT SERVES AS THE PRINTING AREA.
  24.         Dim printAreaRec As New RectangleF(leftmargin, topmargin, printwidthInt, printheightInt)
  25.  
  26.         ' INSTANTIATE THE STRINGFORMAT CLASS THAT CONTAINS TEXT LAYOUT INFORMATION,
  27.         'DISPLAY MANIPULATIONS AND OPENTYPE FEATURES.
  28.         Dim format As New StringFormat(StringFormatFlags.LineLimit)
  29.  
  30.         Dim linesfilledInt As Int32 'MUST BE PASSED WHEN PASSING CHARFIT.
  31.  
  32.         Dim charsfittedInt As Int32 'USED WHEN CALCULATING CurCharInt AND HasMorePages.
  33.  
  34.         'CALL MEASURESTRING TO KNOW HOW MANY CHARACTERS THAT WILL FIT IN THE PRINTING AREA RECTANGLE.
  35.         e.Graphics.MeasureString(Mid(TextBox1.Text, CurCharInt + 1), fnt, _
  36.                     New SizeF(printwidthInt, printheightInt), format, _
  37.                     charsfittedInt, linesfilledInt)
  38.  
  39.         'IN THIS AREA, THE TEXT WILL BE PRINT IN THE PAGE
  40.         e.Graphics.DrawString(Mid(TextBox1.Text, CurCharInt + 1), fnt, _
  41.             Brushes.Black, printAreaRec, format)
  42.  
  43.         'FORMULA FOR ADVANCING THE CURRENT TO THE LAST CHARACTER PRINTED ON THIS PAGE.
  44.         CurCharInt += charsfittedInt
  45.  
  46.         'CHECKING IF WHETHER THE PRINTING MODULE SHOULD BE FIRE TO ANOTHER PRINTPAGE EVENT
  47.         If CurCharInt < TextBox1.Text.Length Then
  48.             e.HasMorePages = True
  49.         Else
  50.             e.HasMorePages = False
  51.             'RESET CurCharInt AS IT IS STATIC.
  52.             CurCharInt = 0
  53.         End If
  54.     End Sub

Then, double click the Button and do the following code for the Print Preview will appear.

  1. Private Sub btnprev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprev.Click
  2.  
  3.        PrintPreviewDialog1.Document = PrintDocument1 'SET THE DOCUMENT TO PREVIEW
  4.         PrintPreviewDialog1.ShowDialog() 'THE MODAL DIALOG BOX WITH THE CURRENT ACTIVE WINDOW SET AS ITS OWNER
  5.     End Sub

Now, press F5 on the keyboard to run your project.

DOWNLOAD HERE

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!