Print from textbox

Does anyone know how to print i vb
I have information some textbox on a form which i want to send to the printer.


Answer this question

Print from textbox

  • Simon Gittins

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/vbcon/html/vbconprintsupport.asp. You could also check out the PrintForm component from the VB Power packs

    Best regards,
    Johan Stenberg



  • rdkelly

    I want to just simply print text that is within a textbox on the form
    Does anyone know how to to that
    Thanks!!

  • Prateek_cds

    threads merged

  • anukirthi

    Stop, step back a bit, take a deep breath. The problem is that you are using a piece of code without any understanding of any part of it. Hacking on it is not going to get you anywhere, very slowly. Start again: you want to print the text in the text box

    Add a Button, a TextBox and a PrintDocument object to a form. Add the following code to the form:

    Private Sub Button1_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles Button1.Click

    Me.PrintDocument1.Print()

    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _

    ByVal e As System.Drawing.Printing.PrintPageEventArgs) _

    Handles PrintDocument1.PrintPage

    e.Graphics.DrawString(TextBox1.Text, _

    TextBox1.Font, _

    Brushes.Blue, _

    100, 100)

    End Sub

    Now you can press the button to print whatever is typed into the text box to the default printer. Then, follow the links that have been posted previously: it'll tell you a whole lot more about printing.



  • nub340

    Between the links that ahmedilyas and I've suggested, there should be enough information on how to print text (the sample shows how to print the contents of a text file, so you'd have to modify it somewhat to take the text from your textbox instead)

    Another option is to use the PrintForm component as I suggested in a previous answer, create a new form, put a textbox on it, set it to dock-fill, set the text of the textbox and print the form.

    If you could please clarify why you don't think that the information that we have suggested is enough, we may be able to help out some more.

    Best regards,
    Johan Stenberg



  • SParker

    you cant use a streamreader on a textbox, streamreader reads streams/IO not a UI control. so what you would need to do is replace fileToPrint as System.IO.StreamReader with:

    Dim textToPrint as String

    then here:

    fileToPrint = New System.IO.StreamReader(TextBox1.Text)

    should be:

    fileToPrint = Me.TextBox1.Text

    then in your printpage event, change the code to this:

    While count < linesPerPage
    line = Me.textToPrint

    If line Is Nothing Then
    Exit While
    End If
    yPos = topMargin + count * printFont.GetHeight(e.Graphics)
    e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _
    yPos, New StringFormat())
    count += 1
    End While

    there are better ways of doing this but this should get you, hopefully, started. Please also, as suggested previously by Johan, read up the links given as they may well provide a better way of doing the task you are after.




  • Feliner

    take a look at this and see if it helps you, then you can modify it to print whatever you want:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=805077&SiteID=1

    basically you need to use the printdocument/print events/properties/classes

    http://msdn2.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx

    I hope the links will guide you better! (im not an expert on printing!)



  • Edward__

    Does anyone know how to get rid of the box that pops up and says "printing page 1"
    I want it to be invisable.

  • elfenland

    The PrintDocument object controls a lot of how printing is performed. Have a look at the methods and objects associated with that. However, here's how to get rid of that dialog:

    Me.PrintDocument1.PrintController = New Printing.StandardPrintController



  • Mitesh Shah923

    Code Block

    Private Sub Button1_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles Button1.Click

    Me.PrintDocument1.Print()

    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _

    ByVal e As System.Drawing.Printing.PrintPageEventArgs) _

    Handles PrintDocument1.PrintPage

    e.Graphics.DrawString(TextBox1.Text, _

    TextBox1.Font, _

    Brushes.Blue, _

    100, 100)

    End Sub

    why do i get

    Error 3 'PrintDocument1' is not a member of 'AITech_Data_Transfer.Logg'.

    when i try this

    Regards

    //e


  • mihooper1

    Does anyone know how to use code to send information a printer in vb.net
    I want when i click a button it prints information from certain textboxes
    Any thoughts
    Thanks!!

  • Lance77035

    It still dose not print. I'm i doing something wrong
    I replaced the line at the buttom that said "line = fileToPrint.ReadLine()" with "line = textToPrint.ToString"
    I also tried it without that line and it prints two blank pages.
    Any thoughts
    Thanks!!


    Option Strict On
    Imports System.IO
    Public Class Form1
    Private checkPrint As Integer




    ' Dim fileToPrint As System.IO.StreamReader
    Dim printFont As System.Drawing.Font
    Dim textToPrint As String
    Private Sub PrintButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _
    PrintButton.Click
    Dim PrintPath As String = System.Environment. _
    GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
    textToPrint = Me.TextBox1.Text
    'fileToPrint = New System.IO.StreamReader(PrintPath & "\myFile.txt")
    printFont = New System.Drawing.Font("Arial", 10)
    PrintDocument1.Print()
    'fileToPrint.Close()

    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As _
    System.Drawing.Printing.PrintPageEventArgs) Handles _
    PrintDocument1.PrintPage

    Dim linesPerPage As Single = 0
    Dim yPos As Single = 0
    Dim count As Integer = 0
    Dim leftMargin As Single = e.MarginBounds.Left
    Dim topMargin As Single = e.MarginBounds.Top
    Dim line As String = Nothing
    linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
    While count < linesPerPage
    line = textToPrint.ToString

    'line = fileToPrint.ReadLine()
    If line Is Nothing Then
    Exit While
    End If
    yPos = topMargin + count * printFont.GetHeight(e.Graphics)
    e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _
    yPos, New StringFormat())
    count += 1
    End While
    If Not (line Is Nothing) Then
    e.HasMorePages = True
    End If
    End Sub
    End Class

  • Ziation

    The first link took me nowhere and and the second link is not what i am lookink for.

    What i want to do is if i have a button that generates a message to a textbox i want to me able to print the contents of that textbox to my printer.

  • n0n4m3

    Here is my code. I got this from a search and i am trying to change it so that it prints from the textbox and not the file. I have a textbox on the form called textbox1.
    When i run the code with the piece i entered it tells me "Argument Exception was unhandled"
    Any thoughts
    Thanks!!

    Option Strict On
    Imports System.IO
    Public Class Form1


    Dim fileToPrint As System.IO.StreamReader
    Dim printFont As System.Drawing.Font
    Private Sub PrintButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _
    PrintButton.Click
    Dim PrintPath As String = System.Environment. _
    GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
    fileToPrint = New System.IO.StreamReader(TextBox1.Text)
    'fileToPrint = New System.IO.StreamReader(PrintPath & "\myFile.txt")
    printFont = New System.Drawing.Font("Arial", 10)
    PrintDocument1.Print()
    fileToPrint.Close()
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As _
    System.Drawing.Printing.PrintPageEventArgs) Handles _
    PrintDocument1.PrintPage
    Dim linesPerPage As Single = 0
    Dim yPos As Single = 0
    Dim count As Integer = 0
    Dim leftMargin As Single = e.MarginBounds.Left
    Dim topMargin As Single = e.MarginBounds.Top
    Dim line As String = Nothing
    linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
    While count < linesPerPage
    line = fileToPrint.ReadLine()
    If line Is Nothing Then
    Exit While
    End If
    yPos = topMargin + count * printFont.GetHeight(e.Graphics)
    e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _
    yPos, New StringFormat())
    count += 1
    End While
    If Not (line Is Nothing) Then
    e.HasMorePages = True
    End If
    End Sub
    End Class

  • Print from textbox