7 Replies - 1630 Views - Last Post: 22 January 2014 - 06:36 AM Rate Topic: -----

#1 cmclemore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-October 12

Form printing question

Posted 01 October 2012 - 08:31 AM

I am very new to VB.net and have learned that my degree really didn't prepare me for what I would encounter in the "real world". I am looking for some assistance with printing a windows form, and will give any and all details of this project.

I am modifying another's project that was created using Visual Studio 2008, and I am currently using Visual Studio 2010. When the project converted from 2008->2010, there were no errors encountered.

I followed the tutorial on this site (pasted link below), but was written a bit ago and I am just wondering if VS 2010 is different in some manner.
http://www.dreaminco...nting-in-vbnet/

This may be too much detail, but since I am new, I wanted to make sure to cover all the bases. This application will open a file from using the menu strip (Data), then the data from the file will be processed, and appear in all textboxes on the TestResults user control, that is on a TabPage.

I have added a Print menu item, and then a Print Form menu item. May be redundant at this point, but for starters this is how I did it. I added PrintDocument, PrintDialog, and PageSetupDialog from the toolbox to the TestResults user control (I hope you can see that from Screen1.png).

I made my own print class called "cPrintData" and pretty much copy/pasted the code from the tutorial in there:
Option Strict On

Imports System.Drawing
Imports System.Drawing.Printing


Public Class cPrintData : Inherits Printing.PrintDocument
#Region " Property Variables "
    ' <summary>  
    ' Property variable for the Font the user wishes to use  
    ' </summary>  
    ' <remarks></remarks>  
    Private _font As Font
    ' <summary>  
    ' Property variable for the text to be printed  
    ' </summary>  
    ' <remarks></remarks>  
    Private _text As String
#End Region

#Region " Class Properties "
    ' <summary>
    ' Property to hold the text that is to be printed
    ' </summary>
    ' <value></value>
    ' <returns>A string</returns>
    ' <remarks></remarks>
    Public Property TextToPrint() As String
        Get
            Return _text
        End Get
        Set(ByVal Value As String)
            _text = Value
        End Set
    End Property

    ' <summary>
    ' Property to hold the font the users wishes to use
    ' </summary>
    ' <value></value>
    ' <returns>A font</returns>
    ' <remarks></remarks>
    Public Property PrinterFont() As Font
        ' Allows the user to override the default font
        Get
            Return _font
        End Get
        Set(ByVal Value As Font)
            _font = Value
        End Set
    End Property
#End Region

#Region " Class Constructors "
    ' <summary>
    ' Empty constructor
    ' </summary>
    ' <remarks></remarks>
    Public Sub New()
        'Call the base classes constructor
        MyBase.New()
        'Instantiate out Text property to an empty string
        _text = String.Empty
    End Sub

    ' <summary>
    ' Constructor to initialize our printing object
    ' and the text it's supposed to be printing
    ' </summary>
    ' <param name="str">Text that will be printed</param>
    ' <remarks></remarks>
    Public Sub New(ByVal str As String)
        'Call the base classes constructor
        MyBase.New()
        'Set our Text property value
        _text = str
    End Sub
#End Region

#Region " onbeginPrint "
    ' <summary>
    ' Override the default onbeginPrint method of the PrintDocument Object
    ' </summary>
    ' <param name="e"></param>
    ' <remarks></remarks>
    Protected Overrides Sub onbeginPrint(ByVal e As Printing.PrintEventArgs)
        ' Run base code
        MyBase.onbeginPrint(e)

        'Check to see if the user provided a font
        'if they didnt then we default to Times New Roman
        If _font Is Nothing Then
            'Create the font we need
            _font = New Font("Times New Roman", 10)
        End If
    End Sub
#End Region

#Region " OnPrintPage "
    ' <summary>
    ' Override the default OnPrintPage method of the PrintDocument
    ' </summary>
    ' <param name="e"></param>
    ' <remarks>This provides the print logic for our document</remarks>
    Protected Overrides Sub OnPrintPage(ByVal e As Printing.PrintPageEventArgs)
        ' Run base code
        MyBase.OnPrintPage(e)

        'Declare local variables needed
        Static curChar As Integer
        Dim printHeight As Integer
        Dim printWidth As Integer
        Dim leftMargin As Integer
        Dim rightMargin As Integer
        Dim lines As Int32
        Dim chars As Int32

        'Set print area size and margins
        With MyBase.DefaultPageSettings
            printHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            printWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
            leftMargin = .Margins.Left 'X
            rightMargin = .Margins.Top   'Y
        End With

        'Check if the user selected to print in Landscape mode
        'if they did then we need to swap height/width parameters
        If MyBase.DefaultPageSettings.Landscape Then
            Dim tmp As Integer
            tmp = printHeight
            printHeight = printWidth
            printWidth = tmp
        End If

        'Now we need to determine the total number of lines
        'we're going to be printing
        Dim numLines As Int32 = CInt(printHeight / PrinterFont.Height)

        'Create a rectangle printing are for our document
        Dim printArea As New RectangleF(leftMargin, rightMargin, printWidth, printHeight)

        'Use the StringFormat class for the text layout of our document
        Dim format As New StringFormat(StringFormatFlags.LineLimit)

        'Fit as many characters as we can into the print area      

        e.Graphics.MeasureString(_text.Substring(RemoveZeros(curChar)), PrinterFont, New SizeF(printWidth, printHeight), format, chars, lines)

        'Print the page
        e.Graphics.DrawString(_text.Substring(RemoveZeros(curChar)), PrinterFont, Brushes.Black, printArea, format)

        'Increase current char count
        curChar += chars

        'Detemine if there is more text to print, if
        'there is the tell the printer there is more coming
        If curChar < _text.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
            curChar = 0
        End If
    End Sub
#End Region

#Region " RemoveZeros "
    ' <summary>
    ' Function to replace any zeros in the size to a 1
    ' Zero's will mess up the printing area
    ' </summary>
    ' <param name="value">Value to check</param>
    ' <returns></returns>
    ' <remarks></remarks>
    Public Function RemoveZeros(ByVal value As Integer) As Integer
        'Check the value passed into the function,
        'if the value is a 0 (zero) then return a 1,
        'otherwise return the value passed in
        Select Case value
            Case 0
                Return 1
            Case Else
                Return value
        End Select
    End Function
#End Region

End Class




Then on the form called "ChemLabResultsView" I added:
#Region " PrintDocument "
    Public Sub PrintDocument()
        'Create an instance of our printer class
        Dim printer As New cPrintData()
        'Set the font we want to use
        printer.PrinterFont = New Font("Verdana", 10)
        'Set the TextToPrint property
        printer.TextToPrint = TextBox1.Text()
        'Issue print command
        printer.Print()
    End Sub
#End Region

    Private Sub mnuPrintForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrintForm.Click
        PrintDocument()
    End Sub


I will attach (2) screen shots of the application, and Screen2.png is the particular part that I would like to print. Please let me know if I did not give enough data/information!

Thanks in advance,
Charlene

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Form printing question

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Form printing question

Posted 01 October 2012 - 08:37 AM

If you're just wanting to print the actual form (screen2) the Visual Basic PowerPacks (which I think are included in the 2010 toolbox) has a PrintForm control that may work for this.

This post has been edited by CharlieMay: 01 October 2012 - 08:37 AM

Was This Post Helpful? 1
  • +
  • -

#3 cmclemore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-October 12

Re: Form printing question

Posted 01 October 2012 - 09:48 AM

Thanks CharlieMay for your quick response. I followed the PowerPacks instructions here:
http://www.dreaminco..._1&#entry510862

And specifically added this line of code after adding the PrintForm PowerPacks control to my form:
Private Sub mnuPrintForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPrintForm.Click
   PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
End Sub



This worked very simply, but the printer output is cut off on the right-hand side of my form. As I am VERY newbie, haha, I didn't realize this was what I had already tried yesterday from googling how to print using VB.net and reading.

Now, since this does not work the way I need it to, I am following the other post here:
http://www.knowdotne.../printform.html

Trying to get it to work as I am typing this. I am having difficulty finding where to paste the code within my project since I do not have a declarations section so to speak. Again, I am modifying existing code from another programmer and it is not structured in a way I am familiar with.

I will post my results once I can get it working and/or ask for more assistance if I cannot. I just wanted to let you know that I am *working* on it and not ignoring your post.
Was This Post Helpful? 0
  • +
  • -

#4 cmclemore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-October 12

Re: Form printing question

Posted 01 October 2012 - 10:45 AM

Okay, the print is working now. It still is cutting a little off of the right-hand side, but I will be playing around with it and see if I can get it right.

Thanks for your help. Sometimes just writing it out seems to help too. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Form printing question

Posted 01 October 2012 - 11:16 AM

You can try setting the margins prior to print. It could be something as simple as
PrintForm1.PrinterSettings.DefaultPageSettings.Margins.Bottom = 5
PrintForm1.PrinterSettings.DefaultPageSettings.Margins.Left = 5
PrintForm1.PrinterSettings.DefaultPageSettings.Margins.Right = 5
PrintForm1.PrinterSettings.DefaultPageSettings.Margins.Top = 5

This post has been edited by CharlieMay: 01 October 2012 - 11:17 AM

Was This Post Helpful? 0
  • +
  • -

#6 cmclemore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-October 12

Re: Form printing question

Posted 01 October 2012 - 12:34 PM

I am assuming here that you are referring to using the Visual Basic PowerPacks again? If so, that is not what I am using now. I thought I stated that in my post, but if not, I apologize.
Was This Post Helpful? 0
  • +
  • -

#7 ricardomxl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-January 14

Re: Form printing question

Posted 21 January 2014 - 11:22 PM

Good day...

I have a problem when a Form tries to printing in VB2010

Desire on the moment to print to select the manual supply, without use the PrintToPreview, directly from a button and on having print request to insert the paper in the printer.

I am grateful for his help

For Each papSource In PrintForm1.PrinterSettings.PaperSources
    If papSource.Kind = PaperSourceKind.ManualFeed Then
       PrintForm1.PrinterSettings.DefaultPageSettings.PaperSource = papSource
       PrintForm1.Print()
       Close()
       Exit For
    End If
Next 

Was This Post Helpful? 0
  • +
  • -

#8 ybadragon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 255
  • View blog
  • Posts: 1,386
  • Joined: 11-May 12

Re: Form printing question

Posted 22 January 2014 - 06:36 AM

What I do to print my form, or part of my form is get a bitmap of the control and save that bitmap as a jpeg. Then create a process object and set it's values to the place where you saved the image. It doesn't require any extra classes, or addons. Mine prints out an image of a chart.

    Private Sub ControlBmpToFile(control As Control, fName As String)
        Dim bmp As New Bitmap(control.Width, control.Height)
        control.DrawToBitmap(bmp, control.DisplayRectangle)
        bmp.Save(fName, ImageFormat.Jpeg)
    End Sub

    Private Sub PrintFile(ByVal fileName As String)
        Dim myFile As New ProcessStartInfo
        myFile.UseShellExecute = True
        myFile.WindowStyle = ProcessWindowStyle.Hidden
        myFile.FileName = fileName
        myFile.Verb = "Print"
        Process.Start(myFile)
    End Sub



This will bring up a dialog like when you right click a file and click print.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1