i'm trying to print a tablelayoutpanel that's inside a Panel using printdocument. But when i preview the print the tablelayoutpanel is placed in the top left corner. I would like to have it where i placed it on the panel. The labels that are on the panel maintain there position but the tablelayoutpanel isn't.
This is the code i'm using:
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim blackBrush As Brush = New SolidBrush(Color.Black)
For Each ctrl As Control In Panel.Controls
e.Graphics.DrawString(ctrl.Text, ctrl.Font, blackBrush, ctrl.Bounds.Location)
Next
For Each ctrl As Control In Tablelayoutpanel.Controls
e.Graphics.DrawString(ctrl.Text, ctrl.Font, blackBrush, ctrl.Bounds.Location)
Next
End Sub
The Location
property of each child control in the TableLayoutPanel
is relative to the TableLayoutPanel
, but you are printing as through it's relative to the Panel
. If, for instance, the TableLayoutPanel
was at (50,50) inside the Panel
and a Label
was at (10,10) within it then the Label
would be at (60,60) relative to the Panel
, so that's where you'd need to print it. The simplest option is probably to convert each of those controls' Location
to screen coordinates and then convert that to client coordinates for the Panel
:
For Each ctrl As Control In Panel.Controls
e.Graphics.DrawString(ctrl.Text, ctrl.Font, blackBrush, ctrl.Bounds.Location)
Next
For Each ctrl As Control In Tablelayoutpanel.Controls
e.Graphics.DrawString(ctrl.Text,
ctrl.Font,
blackBrush,
Panel.PointToClient(Tablelayoutpanel.PointToScreen(ctrl.Bounds.Location)))
Next