[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 ?!
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.
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...
Re: [2008] Printing scrollable Panel object.
something like this:
vb Code:
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 + 15, ctrl.Width, ctrl.Height), sf)
Next
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
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 ?
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
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 ?
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
Re: [2008] Printing scrollable Panel object.
I need to print 3 A4 pages... and shold i use 3 different panels ?!
Re: [2008] Printing scrollable Panel object.
try this:
vb Code:
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Static page As Integer = 1
Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
Static startAt As Integer = 0
For Each ctrl As Control In Me.Panel1.Controls
If Me.Panel1.Controls.GetChildIndex(ctrl) >= startAt Then
If ctrl.Top + ctrl.Height < startPosition + PrintDocument1.DefaultPageSettings.Bounds.Height Then
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 - startPosition, ctrl.Width, ctrl.Height), sf)
Else
startAt = Me.Panel1.Controls.GetChildIndex(ctrl)
page += 1
e.hasmorepages = true
Exit sub
End If
End If
Next
e.hasmorepages = false
End Sub
Re: [2008] Printing scrollable Panel object.
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
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.
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 ??
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:
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
Static page As Integer = 1
Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
Static maxPages As Integer = 0
If page = 1 Then
For Each ctrl As Control In Me.Panel1.Controls
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1
If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag)
End If
Next
End If
For Each ctrl As Control In Me.Panel1.Controls
If val(ctrl.Tag) = page Then
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 = ContentAlignment.TopLeft Then
sf.Alignment = StringAlignment.Near
ElseIf DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopRight Then
sf.Alignment = StringAlignment.Far
End If
End If
sf.FormatFlags = StringFormatFlags.NoClip
e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf)
End If
Next
page += 1
If page > maxPages Then
e.HasMorePages = False
page = 1
maxPages = 0
Else
e.HasMorePages = True
End If
End Sub
End Class
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 ;)
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)
Re: [2008] Printing scrollable Panel object.
vb 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
Static page As Integer = 1
Dim startPosition As Integer = (page - 1) * PrintDocument1.DefaultPageSettings.Bounds.Height
Static maxPages As Integer = 0
If page = 1 Then
For Each ctrl As Control In Me.Panel1.Controls
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Or TypeOf ctrl Is PictureBox Then
ctrl.Tag = Int((ctrl.Top + ctrl.Height) / PrintDocument1.DefaultPageSettings.Bounds.Height) + 1
If CInt(ctrl.Tag) > maxPages Then maxPages = CInt(ctrl.Tag)
End If
Next
End If
For Each ctrl As Control In Me.Panel1.Controls
If CInt(ctrl.Tag) = page Then
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is Label Then
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 = ContentAlignment.TopLeft Then
sf.Alignment = StringAlignment.Near
ElseIf DirectCast(ctrl, Label).TextAlign = ContentAlignment.TopRight Then
sf.Alignment = StringAlignment.Far
End If
End If
sf.FormatFlags = StringFormatFlags.NoClip
e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top - startPosition, ctrl.Width + 50, ctrl.Height), sf)
ElseIf TypeOf ctrl Is PictureBox Then
e.Graphics.DrawImage(DirectCast(ctrl, PictureBox).Image, New PointF(ctrl.Left, ctrl.Top - startPosition))
End If
End If
Next
page += 1
If page > maxPages Then
e.HasMorePages = False
page = 1
maxPages = 0
Else
e.HasMorePages = True
End If
End Sub
End Class
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).
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