|
-
Feb 26th, 2009, 06:31 PM
#1
Thread Starter
Addicted Member
-
Feb 26th, 2009, 06:43 PM
#2
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 26th, 2009, 06:53 PM
#3
Thread Starter
Addicted Member
-
Feb 26th, 2009, 06:59 PM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 26th, 2009, 07:16 PM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2009, 12:43 PM
#6
Thread Starter
Addicted Member
-
Feb 27th, 2009, 12:45 PM
#7
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2009, 02:49 PM
#8
Thread Starter
Addicted Member
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
-
Feb 27th, 2009, 02:52 PM
#9
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2009, 03:10 PM
#10
Thread Starter
Addicted Member
-
Feb 27th, 2009, 03:24 PM
#11
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
Last edited by .paul.; Feb 27th, 2009 at 03:29 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 28th, 2009, 12:59 PM
#12
Re: [2008] Printing scrollable Panel object.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 4th, 2009, 03:37 PM
#13
Thread Starter
Addicted Member
Re: [2008] Printing scrollable Panel object.
 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
-
Mar 4th, 2009, 04:21 PM
#14
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 4th, 2009, 04:35 PM
#15
Thread Starter
Addicted Member
-
Mar 4th, 2009, 10:11 PM
#16
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
Last edited by .paul.; Mar 5th, 2009 at 11:19 AM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 5th, 2009, 04:36 PM
#17
Thread Starter
Addicted Member
-
Mar 5th, 2009, 06:42 PM
#18
Thread Starter
Addicted Member
-
Mar 5th, 2009, 06:58 PM
#19
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 5th, 2009, 07:18 PM
#20
Thread Starter
Addicted Member
-
Oct 20th, 2011, 05:08 AM
#21
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|