printing a hard copy

So i have an app that has text boxes, labels, and comboboxes. the program populates a database. What i need to do now is Print a hard copy of the data (from a command button on the form) and send an email to someone (through Outlook).. Can anyone point me in the right direction on how to do this Thanks.

Ian.



Answer this question

printing a hard copy

  • JavaBoy

    the print dialog class, as it states in the documentation, Lets the user choose which sections of a document to print and what printer to print it to. This is your call, would be good user interaction for the user to select what printer they wish to print to for example

    the printdocument class is the main class that does the print processing job, you have to code in the printpage event, on what to print and how to print it (the format/output), and the documentations also provide a good code snippet on how to achieve all this :-)



  • Louis Y

    can you show us the code you currently have so we can try and "fix" it for you :-)

  • SimonOng

    ok so i got it to print. However, it is not printing the text in the text box... it prints the code in the code window :(... how do i have it print the data the user enters do i need to send the data to a different file or is there a way the streamreader will read the textboxes
  • newguyintown

    emails can be sent programmatically:

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

    as for printing a hardcopy, consider looking at the PrintDocument classes:

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.printdialog.aspx

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

    I hope these links help in some way. Of course, regarding the printdocument class, be sure to change the way it prints meaning instead of printing from a textfile, just get the text from the textboxes/labels etc...



  • omar_rapid

    this was very helpful THANK YOU! i was sending it to a .txt file and printing the .txt file but i think i will use this... THANKS AGAIN!!!!!!
  • Westonm

    it prints out a blank piece of paper... is it even getting to the while loop


  • sharpMoon

    I've managed to make it print (except print it to the file which would be the same thing (same output) as printing to the printer) using the values from the textbox.

    Now its simple code but needs some work on what you maybe wanting to do - this code just goes through each control on the form and gets the textual value and "draws" it to the page. Now its up to you what type of control you want to get the textual value from, such as a textbox or a label or a datetimepicker etc.... but this one does it all, for any control on the form.

    I thought I would post this, perhaps giving you more of a push (and myself) - even though it requires alot of modifications to do what you maybe wanting it to do (print text per line per page) - currently it prints text but only 1 per line instead of a normal document style as you see in an MS Word document for example.


    Private Sub DoPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles thePrintDoc.PrintPage
    Dim linePerPage As Single = 0
    Dim count As Integer = 0
    Dim yPos As Single = 0
    Dim leftMargin As Integer = e.MarginBounds.Left
    Dim rightMargin As Integer = e.MarginBounds.Right

    For Each currentControl As Control In Me.Controls
    linePerPage += e.MarginBounds.Height / currentControl.Font.GetHeight(e.Graphics)
    yPos = e.MarginBounds.Top + count * currentControl.Font.GetHeight(e.Graphics)
    e.Graphics.DrawString(currentControl.Text, currentControl.Font, Brushes.Black, leftMargin, yPos,
    New StringFormat())
    count = count + 1
    Next

    End Sub

    of course, place the code in the correct eventhandler for your print document. I'm still working on this and hopefully we can come up with a solution together



  • mabster

    yup thats the problem - the code snippet in the documentation was just a sample. So it was reading a text file, and now you are reading a file (same effect).

    you need to not read a file but instead read the textbox:

    so instead of this:

     



    While count < linesPerPage
    line = streamToPrint.ReadLine()
    If line Is Nothing Then

    Exit While

    End If

    yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
    ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos,
    New StringFormat())
    count += 1
    End While

     

    try this: (untested)

     


    'While count < linesPerPage
    'line = streamToPrint.ReadLine()
    'If line Is Nothing Then

    'Exit While

    'End If

    yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
    ev.Graphics.DrawString(Me.theTextBox1.Text, printFont, Brushes.Black, leftMargin, yPos,
    New StringFormat())
    ev.Graphics.DrawString(Me.theTextBox2.Text, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
    ev.Graphics.DrawString(Me.theTextBox3.Text, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
    'count += 1
    'End While


     

     

    You could probably do away with the whole "linesPerPage" but we will deal with that later.

    I hope the above helps you..... :-)



  • Daniel Ratcliffe

    sure:

    Imports System

    Imports System.IO

    Imports System.Drawing

    Imports System.Drawing.Printing

    Imports System.Windows.Forms

    Public Class new_empl_entry_1

    Inherits System.Windows.Forms.Form

    Private components1 As System.ComponentModel.Container

    Private printFont As Font

    Private streamToPrint As StreamReader

    Private WithEvents docToPrint As New Printing.PrintDocument

    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click

    'print dialog (choose what printer to print to)

    PrintDialog1.AllowSomePages = True

    PrintDialog1.ShowHelp = True

    PrintDialog1.Document = docToPrint

    Dim result As DialogResult = PrintDialog1.ShowDialog()

    If (result = Windows.Forms.DialogResult.OK) Then

    docToPrint.Print()

    End If

    'print document(what is acutally being printed)

    Try

    (this is where i believe the problem is... i realize that calling "form1.vb" prints the code (since i did it :/ ) but i also assume that this is where i would need to tell it to read the text boxes instead or is that wrong )

    streamToPrint = New StreamReader("C:\Documents and Settings\igiberson\Desktop\VB projects\Newest Employee\Newest Employee\form1.vb")

    Try

    printFont = New Font("Arial", 10)

    Dim pd As New PrintDocument()

    AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage

    pd.Print()

    Finally

    streamToPrint.Close()

    End Try

    Catch ex As Exception

    MessageBox.Show(ex.Message)

    End Try

    End Sub

    Public Sub New()

    ' The Windows Forms Designer requires the following call.

    InitializeComponent()

    End Sub

    Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)

    Dim linesPerPage As Single = 0

    Dim yPos As Single = 0

    Dim count As Integer = 0

    Dim leftMargin As Single = ev.MarginBounds.Left

    Dim topMargin As Single = ev.MarginBounds.Top

    Dim line As String = Nothing

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

    While count < linesPerPage

    line = streamToPrint.ReadLine()

    If line Is Nothing Then

    Exit While

    End If

    yPos = topMargin + count * printFont.GetHeight(ev.Graphics)

    ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())

    count += 1

    End While

    If (line IsNot Nothing) Then

    ev.HasMorePages = True

    Else

    ev.HasMorePages = False

    End If

    End Sub

    End Class


  • wsaunders

    should i be using both the printdialog and printdocument classes in order to print properly
  • RC COLA

    ok so that didnt work... i was wondering... can i program it so that it prints the access database file that it populates and would that be like the example only with the database file path instead


  • lk_spec

    how do i choose for it to only read certain controls and not all of them I.E. only the textboxes and labels, not the buttons...
  • Hemant Kumar

    no that still wouldn't work since its a different file format (special). What doesn't work what happens when you modify the code as above

  • aDarren

    well following examples and tinkering I think I'm closer but im sure there is a much better way than this but I will post. Followed example and implemented it into the code using:

    http://msdn2.microsoft.com/en-us/library/cwbe712d.aspx

    So, we declare a string variable globally to store the contents we want to print.

    Dim theContentsToPrint as String = String.Empty

    then we of course implement our print event/printpage event handlers.

    Now when we are going to print at the press of a button for example, in this button we use the global variable to store the content we want to print. Something like this, again, this is going through each control on the form and getting its textual property and appending to it. You can also, again, decide which type of controls to get the text from (textboxes alone, or maybe just labels...whatever) - this is optional.

    So in this button click for printing:

     



    for each currentControl as Control in Me.Controls
       Me.theContentsToPrint = Me.theContentsToPrint + currentControl.Text + " " 'just an extra space just in case
    next

     

     

    Now when we print, in the printpage event, code would be this:

     



    Dim theLinesPerPage as Integer = 0
    Dim theCharsOnPage as Integer = 0
     
    e.Graphics.MeasureString(Me.theContentsToPrint, Me.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, theCharsOnPage, theLinesPerPage)
    e.Graphics.DrawString(Me.theContentsToPrint, Me.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic)
    Me.theContentsToPrint = Me.theContentsToPrint.Substring(theCharsOnPage)
    e.HasMorePages = Me.theContentsToPrint.Length > 0

     

     

    does this work for you is this what you maybe after Sorry - new to the printing thing myself and not an export so....I'm trying! :-)



  • printing a hard copy