dcsimg
Results 1 to 16 of 16

Thread: Printing using VB.NET

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    22

    Printing using VB.NET

    i have an application that am adding a print option in the menu strip. i want the application to be able to print everything on the form when i hit print. right now it only prints whats displayed on the screen but i want to print everything thats displayed. Here is the code i have have. it only allows me to print whats displayed and scroll down to print more. any help on how to print everything on the form is much appreciated.

    Code:
     Private Sub PrintPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPageToolStripMenuItem.Click
         
    
            Dim settings As New System.Drawing.Printing.PrinterSettings
            PrintForm1.PrinterSettings = settings
            settings.DefaultPageSettings.Landscape = True
    
            PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
    
            PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
    end sub

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,540

    Re: Printing using VB.NET

    PrintForm can be handy, but generally doesn't pan out. But what you're wanting it to do can be done with PrintForm. In the .Print you set the Form and the PrintOption correctly, the only thing that I can assume is that you have the form's AutoScroll property set to False. Change it to True and see if that works for you.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    22

    Re: Printing using VB.NET

    Thank you for the reply dday9 i did have the form auto scroll on false however that still didn't fix my problem.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,540

    Re: Printing using VB.NET

    Hmm, how is it not displaying correctly? Is it not showing controls that you expect to be there or is it still only showing the visible portion of the screen? The reason I ask is because look at the differences:

    1)
    Code:
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            'Without AutoScroll enabled
            PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
    
            PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
        End Sub
    Name:  without.png
Views: 1893
Size:  6.3 KB

    2)
    Code:
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            'With AutoScroll enabled
            Me.AutoScroll = True
    
            PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
    
            PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
        End Sub
    Name:  with.png
Views: 1973
Size:  10.7 KB

    You can see that in the second photo, the label at the bottom is now appearing once AutoScroll is enabled.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    22

    Re: Printing using VB.NET

    i see how yours works but i did the exact same thing and it still prints whats displayed and never scrolls down to print the rest.

    Code:
     Private Sub PrintPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPageToolStripMenuItem.Click
         
            Dim settings As New System.Drawing.Printing.PrinterSettings
            Me.AutoScroll = True
            PrintForm1.PrinterSettings = settings
            settings.DefaultPageSettings.Landscape = True
    
            PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
    
            PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
    
        End Sub

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    22

    Re: Printing using VB.NET

    It still prints off the visible portion of the screen and that's it.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    22

    Re: Printing using VB.NET

    if it helps here is a picture of what im trying to print. Name:  printing.jpg
Views: 2035
Size:  201.0 KB

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,540

    Re: Printing using VB.NET

    Are you using the automatic scrollbars to scroll through the form, or are you using a v_scroll bar to scroll down?

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    22

    Re: Printing using VB.NET

    I believe its automatic. I also read some stuff on printform1 and everything i read was saying that it only prints whats displayed on screen and the best way to do what i want to do is by print document. Do you have an idea how to do that?

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,540

    Re: Printing using VB.NET

    PrintDocument is much more difficult to use than PrintForm, however it gives you so much more control on what you're printing. The place that I'll direct you to is Merrion's printing guide, ---> here <---. It's everything you need to know and more on how to print in Visual Basic.Net.

  11. #11
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Printing using VB.NET

    Let's at least get the PrintForm confusion out of the way. It's not that it's only showing the visible area at all. The paper simply isn't big enough to get any more of the form into. PrintForm merely reproduces the form actual size. If it runs off the edge of the paper then that bit's simply lost. You'd get more in by reducing margins and using the whole printable area but that still might not be enough.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    22

    Re: Printing using VB.NET

    do you guys have any ideas on how to get the whole form to print and it can be on more than 1 page as long as everything on the form prints.

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,540

    Re: Printing using VB.NET

    Mike, I take it that you didn't go through that thread that I provided in post #10. Even in the PrintDocument documentation, it gives an example on how to handle multiple pages.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    22

    Re: Printing using VB.NET

    I did read it and i also played around with it all day yesterday and also found this other code that im trying to change according to what i have and i still cant figure it out. In this code they are trying to print text from a textbox and not graphics. Here is the code.

    Code:
     Private Sub PrintPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPageToolStripMenuItem.Click
            Me.AutoScroll = True
            StringToPrint = Textbox.Text
            PrintDocument1.Print()
        End Sub
    
        Private Sub PrintForm1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim numChars As Integer
            Dim numLines As Integer
            Dim stringForPage As String
            Dim strFormat As New StringFormat()
            Dim PrintFont As Font
            PrintFont = Textbox.Font
            Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
            Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
            strFormat.Trimming = StringTrimming.Word
            e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)
            stringForPage = StringToPrint.Substring(0, numChars)
            e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)
            If numChars < StringToPrint.Length Then
                StringToPrint = StringToPrint.Substring(numChars)
                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If
        End Sub

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Posts
    22

    Re: Printing using VB.NET

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

    that PrintForm1 should be PrintDocument1

  16. #16
    Junior Member
    Join Date
    Jul 2019
    Posts
    18

    Re: Printing using VB.NET

    Quote Originally Posted by mike2013 View Post
    Private Sub PrintForm1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

    that PrintForm1 should be PrintDocument1
    Were you able to solved it?. I also have similar issues by printing scrollable panel containing listview. Help me if your worked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width