Skip to content
MPGHThe Dark Arts
/
RegisterLog in
Forum
Community
What's NewLatest posts across the boardTrendingHottest threads right nowSubscribedThreads you follow
Discussion
GeneralIntroductionsEntertainmentDebate FortFlaming & Rage
Board
News & AnnouncementsMPGH TimesSuggestions & HelpGiveaways
More Sections
Art & Graphic DesignProgrammingHackingCryptocurrency
Hacks & Cheats
Games
ValorantCS2 / CS:GOCall of Duty / WarzoneFortniteApex LegendsEscape From Tarkov
+14 moreLeague of LegendsGTA VMinecraftRustROTMGBattlefieldTroveBattleOnCombat ArmsCrossFireBlackshotRuneScapeDayZDead by Daylight
Resources
Game Hacking TutorialsReverse EngineeringGeneral Game HackingAnti-CheatConsole Game Hacking
Tools
Game Hacking ToolsTrainers & CheatsHack/Release NewsNew
Submit a release →Share your cheat, tool, or config with the community.
AINEW
AI Tools
General & DiscussionPrompt EngineeringLLM JailbreaksHotAI Agents & AutomationLocal / Open Models
AI × Gaming
AI Aimbots & VisionML Anti-CheatGame Bots & Automation
Create
AI Coding / Vibe CodingAI Art & MediaAI Voice & TTS
The AI frontier →Where game hacking meets modern machine learning. Jump in.
Marketplace
Buy & Sell
SellingBuyingTradingUser Services
Trust & Safety
Middleman LoungeMarketplace TalkVouch Copy Profiles
Money
Cryptocurrency TalkCurrency ExchangeWork & Job Offers
Start selling →List accounts, services, and goods. Use the middleman to trade safe.
MPGH The Dark Arts

A community for offensive security research, reverse engineering, and AI.

Community

ForumMarketplaceSearch

Account

RegisterLog in

Legal

Privacy PolicyForum RulesHelp & FAQ
© 2026 MPGH · All rights reserved.Built by the community, for the community. For educational purposes onlyContent is shared for security research and education — we don't condone illegal use. You're responsible for complying with applicable laws. Use at your own risk.
Home › Forum › Programming › Visual Basic Programming › [Help]Print Text Box [Solved]

[Help]Print Text Box [Solved]

Posts 1–8 of 8 · Page 1 of 1
zmansquared
zmansquared
[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
#1 · 16y ago
MJLover
MJLover
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 !!
#2 · 16y ago
zmansquared
zmansquared
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
#3 · 16y ago
MJLover
MJLover
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 !!!
#4 · 16y ago
NextGen1
NextGen1
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


#5 · edited 16y ago · 16y ago
zmansquared
zmansquared
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
#6 · 16y ago
NextGen1
NextGen1


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
#7 · 16y ago
zmansquared
zmansquared
But where does the textbox code go? textbox or where. I am lost, can you string the code together for me?
#8 · 16y ago
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

  • [Help]Basic Text Box[Solved]By FlashDrive in Visual Basic Programming
    3Last post 16y ago
  • [Help]Printing Web Page[Solved]By zmansquared in Visual Basic Programming
    7Last post 16y ago
  • [Help]Check a Box[Solved]By ppl2pass in Visual Basic Programming
    6Last post 16y ago
  • [Help]Changing text values[Solved]By master131 in Visual Basic Programming
    4Last post 15y ago
  • [Help]Save Text to Desktop[Solved]By ppl2pass in Visual Basic Programming
    8Last post 16y ago

Tags for this Thread

None