How to Print the Text in a TextBox in VB.net with Source Code

How to Print the Text in a TextBox in VB.net with Source Code

This tutorial is all about 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.

What is Visual Basic’s purpose?

The third-generation programming language was created to aid developers in the creation of Windows applications. It has a programming environment that allows programmers to write code in.exe or executable files. They can also utilize it to create in-house front-end solutions for interacting with huge databases. Because the language allows for continuing changes, you can keep coding and revising your work as needed.

However, there are some limits to the Microsoft Visual Basic download. If you want to make applications that take a long time to process, this software isn’t for you. That implies you won’t be able to use VB to create games or large apps because the system’s graphic interface requires a lot of memory and space. Furthermore, the language is limited to Microsoft and does not support other operating systems.

What are the most important characteristics of Visual Basic?

Microsoft Visual Basic for Applications Download, unlike other programming languages, allows for speedier app creation. It has string processing capabilities and is compatible with C++, MFC, and F#. Multi-targeting and the Windows Presentation Framework are also supported by the system, allowing developers to create a variety of Windows apps, desktop tools, metro-style programs, and hardware drivers.

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.

[vbnet]
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
‘DECLARE A STATIC VARIABLE
‘HOLD THE POSITION OF THE LAST PRINTED CHARACTER
‘AND IT CAN REFERECE THE SUBSEQUET PRINTPAGE EVENTS.
Static CurCharInt As Int32
‘SET THE FONT THAT YOU WANT TO USE FOR PRINTING.
Dim fnt As New Font(“Segoe UI”, 20)
Dim printheightInt, printwidthInt, leftmargin, topmargin As Int32
With PrintDocument1.DefaultPageSettings
‘ SET THE VARIABLE THAT HOLD THE LIMITS OF THE PRINTING AREA RECTANGLE
printheightInt = .PaperSize.Height – .Margins.Top – .Margins.Bottom
printwidthInt = .PaperSize.Width – .Margins.Left – .Margins.Right

‘ SET THE VARIABLES TO HOLD THE VALUES OF THE MARGIN THAT WILL WORK FOR
‘THE X AND Y COORDINATES OF THE UPPER LEFT CORNER OF THE PRINTING AREA RECTANGLE
leftmargin = .Margins.Left ‘ X coordinate
topmargin = .Margins.Top ‘ Y coordinate
End With

‘ COMPUTE THE TOTAL LINES IN THE DOCUMENT BASED ON THE HEIGHT OF THE FONT AND ITS PRINTING AREA.
Dim linecountInt As Int32 = CInt(printheightInt / fnt.Height)

‘ SET THE RECTANGLE STRUCTURE THAT SERVES AS THE PRINTING AREA.
Dim printAreaRec As New RectangleF(leftmargin, topmargin, printwidthInt, printheightInt)

‘ INSTANTIATE THE STRINGFORMAT CLASS THAT CONTAINS TEXT LAYOUT INFORMATION,
‘DISPLAY MANIPULATIONS AND OPENTYPE FEATURES.
Dim format As New StringFormat(StringFormatFlags.LineLimit)

Dim linesfilledInt As Int32 ‘MUST BE PASSED WHEN PASSING CHARFIT.

Dim charsfittedInt As Int32 ‘USED WHEN CALCULATING CurCharInt AND HasMorePages.

‘CALL MEASURESTRING TO KNOW HOW MANY CHARACTERS THAT WILL FIT IN THE PRINTING AREA RECTANGLE.
e.Graphics.MeasureString(Mid(TextBox1.Text, CurCharInt + 1), fnt, _
New SizeF(printwidthInt, printheightInt), format, _
charsfittedInt, linesfilledInt)

‘IN THIS AREA, THE TEXT WILL BE PRINT IN THE PAGE
e.Graphics.DrawString(Mid(TextBox1.Text, CurCharInt + 1), fnt, _
Brushes.Black, printAreaRec, format)

‘FORMULA FOR ADVANCING THE CURRENT TO THE LAST CHARACTER PRINTED ON THIS PAGE.
CurCharInt += charsfittedInt

‘CHECKING IF WHETHER THE PRINTING MODULE SHOULD BE FIRE TO ANOTHER PRINTPAGE EVENT
If CurCharInt < TextBox1.Text.Length Then
e.HasMorePages = True
Else
e.HasMorePages = False
‘RESET CurCharInt AS IT IS STATIC.
CurCharInt = 0
End If
End Sub
[/vbnet]

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

[vbnet]
Private Sub btnprev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprev.Click

PrintPreviewDialog1.Document = PrintDocument1 ‘SET THE DOCUMENT TO PREVIEW
PrintPreviewDialog1.ShowDialog() ‘THE MODAL DIALOG BOX WITH THE CURRENT ACTIVE WINDOW SET AS ITS OWNER
End Sub
[/vbnet]

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

DOWNLOAD HERE

Readers might read also:

If you have any questions or suggestions about this system project or How to Print the Text in a TextBox in VB.net with Source Code, just send your inquiry using our contact page or simply leave a comment below.

Leave a Comment