Results 1 to 21 of 21

Thread: [2008] Printing scrollable Panel object.

  1. #1

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    [2008] Printing scrollable Panel object.

    Hello 2 you all again... and thanks...

    Now i came with another problem...

    I want to print a Panel Object with scroll bars (AutoScroll = True).

    I have searched over the vbforums.com and i have googled for almost 2 days and i did not find any solution for this...

    Anyone can help me with this ?!
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Printing scrollable Panel object.

    what does the panel contain?

    you won't be able to take a snapshot of the panel + print it as an image. you'll probably have to do it all manually.

  3. #3

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] Printing scrollable Panel object.

    it contains labels and textboxes without border... to be transparent on printing solution that i've did'nt found...
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Printing scrollable Panel object.

    something like this:

    vb Code:
    1. For Each ctrl As Control In Me.Panel1.Controls
    2.     Dim sf As New System.Drawing.StringFormat
    3.     If TypeOf ctrl Is TextBox Then
    4.         If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
    5.             sf.Alignment = StringAlignment.Far
    6.         Else
    7.             sf.Alignment = StringAlignment.Near
    8.         End If
    9.     elseif TypeOf ctrl Is Label Then
    10.         If DirectCast(ctrl, Label).TextAlign = HorizontalAlignment.Right Then
    11.             sf.Alignment = StringAlignment.Far
    12.         Else
    13.             sf.Alignment = StringAlignment.Near
    14.         End If
    15.     End If
    16.     e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top + 15, ctrl.Width, ctrl.Height), sf)
    17. Next

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Printing scrollable Panel object.

    i forgot to mention, you need to add a printdocument control to your project + put that code in the _printpage event

  6. #6

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] Printing scrollable Panel object.

    thanks .paul. for the reply... i still have a problem with your sugested solution... is not printing the hole panel... it's scrollable one...

    your solution is printing only the visible area of the panel...

    Is there a problem with my VB or the code is not doing this job ?
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Printing scrollable Panel object.

    its supposed to print all the controls in the panel. i can't see any reason it wouldn't

  8. #8

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] Printing scrollable Panel object.

    I have used just like this...

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            PrintDocument1.Print()
        End Sub
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            For Each ctrl As Control In Me.Panel1.Controls
                Dim sf As New System.Drawing.StringFormat
                If TypeOf ctrl Is TextBox Then
                    If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
                        sf.Alignment = StringAlignment.Far
                    Else : sf.Alignment = StringAlignment.Near
                    End If
                ElseIf TypeOf ctrl Is Label Then
                    If DirectCast(ctrl, Label).TextAlign = HorizontalAlignment.Right Then
                        sf.Alignment = StringAlignment.Far
                    Else
                        sf.Alignment = StringAlignment.Near
                    End If
                End If
                e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top + 25, ctrl.Width, ctrl.Height), sf)
            Next
        End Sub
    End Class
    Might be a problem if the Panel's size is much biger than an A4 format ? I mean i can print more than a A4 page from this panel ? Or only one ?
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Printing scrollable Panel object.

    yeah i didn't allow for more than 1 a4 page. you need to calculate how many a4 pages you have + print them 1 by 1

  10. #10

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] Printing scrollable Panel object.

    I need to print 3 A4 pages... and shold i use 3 different panels ?!
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Printing scrollable Panel object.

    try this:

    vb Code:
    1. Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    2.  
    3.     Static page As Integer = 1
    4.     Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
    5.     Static startAt As Integer = 0
    6.  
    7.     For Each ctrl As Control In Me.Panel1.Controls
    8.         If Me.Panel1.Controls.GetChildIndex(ctrl) >= startAt Then
    9.             If ctrl.Top + ctrl.Height < startPosition + PrintDocument1.DefaultPageSettings.Bounds.Height Then
    10.                 Dim sf As New System.Drawing.StringFormat
    11.                 If TypeOf ctrl Is TextBox Then
    12.                     If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
    13.                         sf.Alignment = StringAlignment.Far
    14.                     Else : sf.Alignment = StringAlignment.Near
    15.                     End If
    16.                 ElseIf TypeOf ctrl Is Label Then
    17.                     If DirectCast(ctrl, Label).TextAlign = HorizontalAlignment.Right Then
    18.                         sf.Alignment = StringAlignment.Far
    19.                     Else
    20.                         sf.Alignment = StringAlignment.Near
    21.                     End If
    22.                 End If
    23.                 e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top - startPosition, ctrl.Width, ctrl.Height), sf)
    24.             Else
    25.                 startAt = Me.Panel1.Controls.GetChildIndex(ctrl)
    26.                 page += 1
    27.                 e.hasmorepages = true
    28.                 Exit sub
    29.             End If
    30.         End If
    31.     Next
    32.     e.hasmorepages = false
    33. End Sub
    Last edited by .paul.; Feb 27th, 2009 at 03:29 PM.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Printing scrollable Panel object.

    did that help?

  13. #13

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] Printing scrollable Panel object.

    Quote Originally Posted by .paul.
    did that help?
    Sorry .paul. for the big delay... i have upgraded the IE8 Beta 2 to IE8 RC1 and for a few days i was unable to access vbforums.com. Now i have entered here by using the vbforums.com IP (63.236.*.*) because i don't know what is hapening.

    And now... let's return to my problem...

    Now... the solution it's not working... on the line 9 it's giving me the error "Error 1 Operator '+' is not defined for types 'Integer' and 'System.Drawing.Printing.PageSettings'. C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\Print Multiple Pages\Print Multiple Pages\Form1.vb 15 45 Print Multiple Pages
    "...

    to be more specific...

    here
    Code:
    If ctrl.Top + ctrl.Height < startPosition + PrintDocument1.DefaultPageSettings Then
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Printing scrollable Panel object.

    its a copy + paste error.
    go back to post #11, + copy all of the code, then open wordpad + paste it, then copy again from wordpad + paste into your project.

  15. #15

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] Printing scrollable Panel object.

    Yes... it's my fault... sorry...

    It's printing only the second page. The first one remains clear without any printed Label.

    I am doing something wrong ??
    Last edited by Alexandru_mbm; Apr 19th, 2011 at 02:26 PM.
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Printing scrollable Panel object.

    the problem was either the controls weren't added from top to bottom or somehow the for each loop is iterating through the controls in the wrong order. i solved it by calculating the number of pages you'll need and assigning each control to a page before starting printing the first page.

    the only further problems i can foresee are if you have any controls that are on the borderline between 2 pages, it might cause an error.

    anyway try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         PrintDocument1.Print()
    5.     End Sub
    6.  
    7.     Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    8.  
    9.         Static page As Integer = 1
    10.         Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
    11.         Static maxPages As Integer = 0
    12.  
    13.         If page = 1 Then
    14.             For Each ctrl As Control In Me.Panel1.Controls
    15.                 If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
    16.                     ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1
    17.                     If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag)
    18.                 End If
    19.             Next
    20.         End If
    21.  
    22.         For Each ctrl As Control In Me.Panel1.Controls
    23.             If val(ctrl.Tag) = page Then
    24.                 Dim sf As New System.Drawing.StringFormat
    25.                 If TypeOf ctrl Is TextBox Then
    26.                     If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
    27.                         sf.Alignment = StringAlignment.Far
    28.                     Else
    29.                         sf.Alignment = StringAlignment.Near
    30.                     End If
    31.                 ElseIf TypeOf ctrl Is Label Then
    32.                     If DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopLeft Then
    33.                         sf.Alignment = StringAlignment.Near
    34.                     ElseIf DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopRight Then
    35.                         sf.Alignment = StringAlignment.Far
    36.                     End If
    37.                 End If
    38.                 sf.FormatFlags = StringFormatFlags.NoClip
    39.                 e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf)
    40.             End If
    41.         Next
    42.  
    43.         page += 1
    44.         If page > maxPages Then
    45.             e.HasMorePages = False
    46.             page = 1
    47.             maxPages = 0
    48.         Else
    49.             e.HasMorePages = True
    50.         End If
    51.  
    52.     End Sub
    53. End Class
    Last edited by .paul.; Mar 5th, 2009 at 11:19 AM.

  17. #17

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] Printing scrollable Panel object.

    For the moment the problem is solved... i will not mark the thread as Resolved yet... in case i will have some problems...

    Thanks .paul. i'll make few tests and if it's OK i will mark as Resolved... anyway U'R THE MAN
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  18. #18

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] Printing scrollable Panel object.

    Yes... just as i said before... i'm still having a little problem... into my Panel i've added a PictureBox... how can i print this too ?

    (I have added a masktextbox but this works fine)
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Printing scrollable Panel object.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         PrintDocument1.Print()
    5.     End Sub
    6.  
    7.     Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    8.  
    9.         Static page As Integer = 1
    10.         Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
    11.         Static maxPages As Integer = 0
    12.  
    13.         If page = 1 Then
    14.             For Each ctrl As Control In Me.Panel1.Controls
    15.                 If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is PictureBox Then
    16.                     ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1
    17.                     If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag)
    18.                 End If
    19.             Next
    20.         End If
    21.  
    22.         For Each ctrl As Control In Me.Panel1.Controls
    23.             If CInt(ctrl.Tag) = page Then
    24.                 If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
    25.                     Dim sf As New System.Drawing.StringFormat
    26.                     If TypeOf ctrl Is TextBox Then
    27.                         If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
    28.                             sf.Alignment = StringAlignment.Far
    29.                         Else
    30.                             sf.Alignment = StringAlignment.Near
    31.                         End If
    32.                     ElseIf TypeOf ctrl Is Label Then
    33.                         If DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopLeft Then
    34.                             sf.Alignment = StringAlignment.Near
    35.                         ElseIf DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopRight Then
    36.                             sf.Alignment = StringAlignment.Far
    37.                         End If
    38.                     End If
    39.                     sf.FormatFlags = StringFormatFlags.NoClip
    40.                     e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf)
    41.                 ElseIf TypeOf ctrl Is PictureBox Then
    42.                     e.Graphics.DrawImage(DirectCast(ctrl, PictureBox).Image, New PointF(ctrl.Left, ctrl.Top - startPosition))
    43.                 End If
    44.             End If
    45.         Next
    46.  
    47.         page += 1
    48.         If page > maxPages Then
    49.             e.HasMorePages = False
    50.             page = 1
    51.             maxPages = 0
    52.         Else
    53.             e.HasMorePages = True
    54.         End If
    55.  
    56.     End Sub
    57. End Class

  20. #20

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] Printing scrollable Panel object.

    You realy are the man Thank you .paul. for your big help... and i think this code shold be added to the CodeBank because i have seen many people having problems for printing this "nuts" Scrollable Panel.

    Thanks again my friend! (still open thread... i hope do not disturb you again).
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  21. #21
    New Member
    Join Date
    Oct 2011
    Posts
    1

    Re: [2008] Printing scrollable Panel object.

    hello.tnx so much for your useful code i also have qustion.can also change this code to some panel that pluse text box and label and picture box have group and radio button too,it is so important project im working at so plz plz help me

    tnx already

Posting Permissions

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



Click Here to Expand Forum to Full Width