Printing problem

johmolan

Well-known member
Joined
Oct 11, 2008
Messages
129
Programming Experience
Beginner
Question 1
I have made a windowsform with 21 tabpages.
I need to print these pages but all I seem to find is methods tha uses types of printscreen methods. Is there any other way to solve this?

Question 2

Is there a way to print a table directly from the dataset?
 
I will try to ask one more time,
Is there a way to print out tables in the database directly from the database?

Or is there a way to print the tables og views to a xls file?
 
Question 1
I have made a windowsform with 21 tabpages.
I need to print these pages but all I seem to find is methods tha uses types of printscreen methods. Is there any other way to solve this?

Apart from coding the whole print routine yourself (which shouldnt be that difficult), I cant instantly think of another way.

Question 2

Is there a way to print a table directly from the dataset?

Again, not directly, but could you not use a Crystal Report?
 
I tried to use the Crystal Report, but since I had many columns in my tables the report ended up looking "not readable". I did not find any way to set it up right.

If you have a tip om how to set it up easily i would be very thankful
 
OK, back to question 1 then - print the tab page as it appears on the screen. Here's a short snippet to get you started :-

VB.NET:
Option Explicit On
Option Strict On

Imports system.Drawing.Printing

Public Class Form1

    Private WithEvents pd As New PrintDocument
    Private sfMiddleLeft As New StringFormat

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        sfMiddleLeft.LineAlignment = StringAlignment.Center 'middle
        sfMiddleLeft.Alignment = StringAlignment.Near 'left

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using PP As New PrintPreviewDialog
            PP.Document = pd
            PP.ShowDialog()
        End Using
    End Sub

    Private brBlack As New SolidBrush(Color.Black)
    Private pBlack As New Pen(Color.Black)

    Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
        With e.Graphics
            For Each _Control As Control In Me.Controls 'chose your tab page here
                Select Case True
                    Case TypeOf (_Control) Is Label
                        .DrawString(_Control.Text, _Control.Font, brBlack, _Control.Left, _Control.Top)
                    Case TypeOf (_Control) Is TextBox
                        .DrawRectangle(pBlack, _Control.Left, _Control.Top, _Control.Width, _Control.Height)
                        .DrawString(_Control.Text, _Control.Font, brBlack, New RectangleF(_Control.Left, _Control.Top, _Control.Width, _Control.Height), sfMiddleLeft)
                End Select
            Next
        End With

    End Sub
End Class

You will obviously need to add more to it, and also check for different alignments in textbox etc etc.
 
In one line you say:
For Each _Control As Control In Me.Controls 'chose your tab page here

I guess you meen that I should select or set which tab to print here?

but how do I do that?
 
VB.NET:
For Each _Control As Control In TabPage2.Controls
 
ok thanks, but if I want to print out all tabs. I have to run it several times.
but I guess I will have to "move" the text onto several pages, is that possible?

otherwise the text wikll be on top of eachother making a mess:D
 
If you want to print each tab page out on a new page, that's possible. You set e.HasMorePages = true each time you want to change page, and loop through all the tab pages.
 
Ok I will try that as well,

I tried it out but the previewpage only adds a lot of pages so I guess I put the code in the wrong plase hehe:confused:

One more question, is it possible to set the page to landscape as default?
and what do I have to add to be able to print the data inthe datagrid?
 
Last edited:
I can't seem to get it to print more than one or extremely many pages.
where in the code must I put " e.HasMorePages = true" to make it work?

And it seems to only print out the text in the textboxes and not the data in the datagrids.
anyone knows why?
 
I can't seem to get it to print more than one or extremely many pages.
where in the code must I put " e.HasMorePages = true" to make it work?
The e.HasMorePages = True goes in the PrintPage event. You'll need to repeat the PrintPage event once for all your TabPages, and then set e.HasMorePages = False after you have printed the last one.

And it seems to only print out the text in the textboxes and not the data in the datagrids.
anyone knows why?
Have you actually written the code to print the DataGridViews?
 
I tried to write the code but that did not work out so I must try some more.

"The e.HasMorePages = True goes in the PrintPage event. You'll need to repeat the PrintPage event once for all your TabPages, and then set e.HasMorePages = False after you have printed the last one." Where in the code?

Mine looks like this:

With e.Graphics
For Each _Control As Control In TabPage1.Controls 'chose your tab page here
Select Case True
Case TypeOf (_Control) Is Label
.DrawString(_Control.Text, _Control.Font, brBlack, _Control.Left, _Control.Top)
Case TypeOf (_Control) Is TextBox
.DrawRectangle(pBlack, _Control.Left, _Control.Top, _Control.Width, _Control.Height)
.DrawString(_Control.Text, _Control.Font, brBlack, New RectangleF(_Control.Left, _Control.Top, _Control.Width, _Control.Height), sfMiddleLeft)

End Select
Next
e.HasMorePages = True
'End With

'With e.Graphics
For Each _Control As Control In TabPage2.Controls 'chose your tab page here
Select Case True
Case TypeOf (_Control) Is Label
.DrawString(_Control.Text, _Control.Font, brBlack, _Control.Left, _Control.Top)
Case TypeOf (_Control) Is TextBox
.DrawRectangle(pBlack, _Control.Left, _Control.Top, _Control.Width, _Control.Height)
.DrawString(_Control.Text, _Control.Font, brBlack, New RectangleF(_Control.Left, _Control.Top, _Control.Width, _Control.Height), sfMiddleLeft)

End Select
Next
e.HasMorePages = False
End With
 
keep track of which page you are printing:
VB.NET:
Private index as Integer = 0
PrintPage handler:
VB.NET:
'draw the page tabControl.TabPages(index)
index += 1
e.HasMorePages = index < tabControl.TabCount
index < tabControl.TabCount
This expression is True until index equals TabCount and all pages has been printed. When this happens and expression is False the PrintPage is not called again.
 
somehow if I have not yet opened the the tab page and seen it it will not print out the data in the textboxes, do I really have to open all the tabpages to be able to print out the data in the textboxes or is there another way around it?
 
Back
Top