Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > Components & Controls > Reporting / Printing

Reporting / Printing Discussion on output-creation components such as reporting, pdf, etc.

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-02-2010, 4:28 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0
 
Join Date: Jun 2007
Posts: 23
Reputation: 36
Grega is on a distinguished programming path ahead
Default How to print tabpage silently

I am not sure if this fits in some other forum thread but here goes...


I have an application in which I open larger number of files create a specific report (I fill in values in a usercontrol with 3 tabpages and some textboxes labels and graph on each of them) for each and would like to print this control silently (meaning never to open it for user to view)...

it works great if I open it and print then, but if I only fill the report and try to print this I just get a blank page. (I am printing in a pdf printer to save paper - for now), although here also if I only look at first tabpage and print all 3 of them (but never actually clicking on the rest) I only get a print of first tabpage and the second two pages are again blank.

The code I use to print:

Code:
 
Public Class PrintMe

    Dim printdialog1 As New PrintDialog
    Dim WithEvents PrintDocument1 As New Printing.PrintDocument
    Private bmp As Bitmap
    Public tp() As TabPage
    'Public tc As TabControl
    Private tabpagecount As Integer = 0

    Public Sub Print() 'ByVal _ucreportpreview As ucReportPreview
      
        '_ucreportpreview.TpPS.DrawToBitmap(bmp, _ucreportpreview.TpPS.DisplayRectangle)
        With printdialog1
            'If PrintDocument1.DefaultPageSettings.PaperSize.Width - bmp.Width < 12 Then
            If PrintDocument1.DefaultPageSettings.PaperSize.Width - tp(tabpagecount).Width < 12 Then

                PrintDocument1.DefaultPageSettings.Margins.Left = Convert.ToInt32((3.0 / 25.4) * 100)
                PrintDocument1.DefaultPageSettings.Margins.Right = Convert.ToInt32((3.0 / 25.4) * 100)
            Else
                'PrintDocument1.DefaultPageSettings.Margins.Left = (PrintDocument1.DefaultPageSettings.PaperSize.Width - bmp.Width) / 2
                PrintDocument1.DefaultPageSettings.Margins.Left = (PrintDocument1.DefaultPageSettings.PaperSize.Width - tp(tabpagecount).Width) / 2
                PrintDocument1.DefaultPageSettings.Margins.Right = PrintDocument1.DefaultPageSettings.Margins.Left

            End If

            PrintDocument1.DefaultPageSettings.Margins.Top = Convert.ToInt32((3.0 / 25.4) * 100)
            'If PrintDocument1.DefaultPageSettings.PaperSize.Height - bmp.Height < 12 Then
            If PrintDocument1.DefaultPageSettings.PaperSize.Height - tp(tabpagecount).Height < 12 Then

                PrintDocument1.DefaultPageSettings.Margins.Bottom = Convert.ToInt32((3.0 / 25.4) * 100)
            Else
                'PrintDocument1.DefaultPageSettings.Margins.Bottom = PrintDocument1.DefaultPageSettings.PaperSize.Height - bmp.Height
                PrintDocument1.DefaultPageSettings.Margins.Bottom = PrintDocument1.DefaultPageSettings.PaperSize.Height - tp(tabpagecount).Height

            End If
            .Document = PrintDocument1
            If printdialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                PrintDocument1.Print()
            End If
        End With

    End Sub


    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        bmp = New Bitmap(tp(tabpagecount).Width, tp(tabpagecount).Height)
        tp(tabpagecount).DrawToBitmap(bmp, tp(tabpagecount).DisplayRectangle)

        tabpagecount += 1
        e.Graphics.DrawImage(bmp, e.MarginBounds)

        If tabpagecount < tp.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
            bmp = Nothing
        End If
    End Sub

End Class

Any help is greatly appreciated.

Regards,
Greg
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-02-2010, 4:49 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.5
 
Join Date: Jan 2010
Age: 38
Posts: 4
Reputation: 0
J. Bijvoets is on a distinguished programming path ahead
Default

Where in your code do you tell the tabpage tp() what is on each tab-page?

If you define a tabcontrol on your form, fill it with controls and data, then print this tabcontrol, your code should work fine, and so it does, doen't it?

The tp() you print in your code shown hasn't got anything on it, so it prints a blank, I suppose.

If I miss something, tell me, and I will look further.

Good luck,

Joris,
author of Developers Challenges
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-02-2010, 4:53 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0
 
Join Date: Jun 2007
Posts: 23
Reputation: 36
Grega is on a distinguished programming path ahead
Default

thanks for reply

the code I posted earlier is in a class PrintMe
here is what I do on my form on which my usercontrol with tabcontrol and tabpages reside!.

Hope this is more clear now.

Greg

Code:
Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
        Dim _PrintMe As New PrintMe
        If Me._UcReportPreview.TabControl1.TabPages.Count > 0 Then
            ReDim _PrintMe.tp(_UcReportPreview.TabControl1.TabPages.Count - 1)
            If _UcReportPreview.TpPS IsNot Nothing Then
                _PrintMe.tp(1) = _UcReportPreview.TpPS
            End If
            If _UcReportPreview.TpSC IsNot Nothing Then
                _PrintMe.tp(0) = _UcReportPreview.TpSC
            End If
            _PrintMe.Print()
        End If
End sub
Quote:
Originally Posted by J. Bijvoets View Post
Where in your code do you tell the tabpage tp() what is on each tab-page?

If you define a tabcontrol on your form, fill it with controls and data, then print this tabcontrol, your code should work fine, and so it does, doen't it?

The tp() you print in your code shown hasn't got anything on it, so it prints a blank, I suppose.

If I miss something, tell me, and I will look further.

Good luck,

Joris,
author of Developers Challenges
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-02-2010, 6:48 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,325
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

What I found when testing this was that the TabPage didn't have to be visible/selected at the time of printing, but it had to be 'shown' at least once during runtime before DrawToBitmap would draw it. So adding this loop in Form.Load resolved that (even though nothing is really shown at Load event).
Code:
For Each page As TabPage In Me.TabControl1.TabPages
    Me.TabControl1.SelectedTab = page
Next
Me.TabControl1.SelectedTab = TabPage1
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-02-2010, 7:10 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0
 
Join Date: Jun 2007
Posts: 23
Reputation: 36
Grega is on a distinguished programming path ahead
Default

Quote:
Originally Posted by JohnH View Post
What I found when testing this was that the TabPage didn't have to be visible/selected at the time of printing, but it had to be 'shown' at least once during runtime before DrawToBitmap would draw it. So adding this loop in Form.Load resolved that (even though nothing is really shown at Load event).
Code:
For Each page As TabPage In Me.TabControl1.TabPages
    Me.TabControl1.SelectedTab = page
Next
Me.TabControl1.SelectedTab = TabPage1
This is it!
I knew posting a question here will give a solution I tried to work this out for two days now without a success.
Thank you very very much!...


Regards,
Greg
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-02-2010, 7:28 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0
 
Join Date: Jun 2007
Posts: 23
Reputation: 36
Grega is on a distinguished programming path ahead
Default

I was a little fast with my previous reply... it isn't 100% yet.

This works when I show a form with usercontrol on which I have tabcontrol and more than 1 tabpage. So it prints all tabpages although I only have first visible without clicking and showing any other tabpage.

But If I dynamically create usercontrol and do everything except bind it to a form, it still prints only blank pages.

this is the code where i dynamically prepare data on a usercontrol.


Code:
 Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
        Dim _ucDataView As ucDataView
        For Each row As DataGridViewRow In DgvLoadedFiles.Rows
            If row.Cells(1).Value = True Or row.Cells(2).Value = True Or row.Cells(3).Value = True Then
                For Each ctr As Control In Frm_Main.TbcViewer.TabPages(row.Index + 1).Controls
                    If TypeOf ctr Is ucDataView Then
                        _ucDataView = DirectCast(ctr, ucDataView)
                        'Dim fp As New Frm_Preview
                        Dim _PrintMe As New PrintMe
                        Dim _ucreportpreview As New ucReportPreview
                        Dim dv As DataView = _ucDataView.DgvStream.DataSource
                        _ucreportpreview.ds = dv.Table.DataSet
                        _ucreportpreview.TabsEnabled(0) = row.Cells(1).Value
                        _ucreportpreview.TabsEnabled(1) = row.Cells(2).Value
                        _ucreportpreview.TabsEnabled(2) = row.Cells(3).Value
                                               _ucreportpreview.Init()
                        For Each page As TabPage In _ucreportpreview.TabControl1.TabPages
                            _ucreportpreview.TabControl1.SelectedTab = page
                        Next
                        _ucreportpreview.TabControl1.SelectedIndex = 0

                        'Print(_ucreportpreview)
                        ReDim _PrintMe.tp(_ucreportpreview.TabControl1.TabPages.Count - 1)
                        Dim c As Integer = 0
                        If row.Cells(1).Value Then
                            _PrintMe.tp(c) = _ucreportpreview.TpDS
                            c += 1
                        End If
                        If row.Cells(2).Value Then
                            _PrintMe.tp(c) = _ucreportpreview.TpSC
                            c += 1
                        End If
                        If row.Cells(3).Value Then
                            _PrintMe.tp(c) = _ucreportpreview.TpPS
                            c += 1
                        End If
                        '_PrintMe.tc = _ucreportpreview.TabControl1
                        _PrintMe.Print()
                    End If
                Next
            End If
        Next
    End Sub
Thanks,
Greg
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-02-2010, 9:21 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,325
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

Call uc.CreateControl() then loop the SelectedTab. All pages should now DrawToBitmap.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-02-2010, 9:33 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0
 
Join Date: Jun 2007
Posts: 23
Reputation: 36
Grega is on a distinguished programming path ahead
Default

Quote:
Originally Posted by JohnH View Post
Call uc.CreateControl() then loop the SelectedTab. All pages should now DrawToBitmap.
I already managed this with adding a line _ucreportpreview.TabControl1.SelectedTab.Show()
in the loop you suggested earlier...but awsn't sure if it was the right way so I will stick with _ucreportpreview.CreateControl() wich also works.

I have one more problem.
on the user control there are tabpages (on 1 I have datagridview and few labels) on second there is a graph few labels and textboxes....if I only show the second tabpage and print it the textboxes are filled in but if I show both tabpages or do this dynamically textboxes are empty although graph draws as it is supposed to and labels have changed text and color.

any Idea what could be wrong here...is there something special about textboxes and printing?

I only noticed this an hour ago so I haven't done much research. I will post a solution if I find one.

Thanks for all the help.

Regards,
Grega


Thanks for all your help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-02-2010, 11:37 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,325
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

Quote:
I have one more problem.
on the user control there are tabpages (on 1 I have datagridview and few labels) on second there is a graph few labels and textboxes....if I only show the second tabpage and print it the textboxes are filled in but if I show both tabpages or do this dynamically textboxes are empty although graph draws as it is supposed to and labels have changed text and color.
I thought the idea here was that you didn't really show the control or any content on it. With the above scenario with a "detached" usercontrol instance with a tabcontrol and multiple tabpages I added a textbox and sets its Text, without that tabpage being currently selected DrawToBitmap included the textbox with text also. So from how I see it the procedure works. Btw, it didn't matter if I set the text before or after doing the SelectedTab loop.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-03-2010, 2:48 AM
VB.NET Forum Newbie
.NET Framework: .NET 2.0
 
Join Date: Jun 2007
Posts: 23
Reputation: 36
Grega is on a distinguished programming path ahead
Default

Quote:
Originally Posted by JohnH View Post
I thought the idea here was that you didn't really show the control or any content on it. With the above scenario with a "detached" usercontrol instance with a tabcontrol and multiple tabpages I added a textbox and sets its Text, without that tabpage being currently selected DrawToBitmap included the textbox with text also. So from how I see it the procedure works. Btw, it didn't matter if I set the text before or after doing the SelectedTab loop.
I didn't make all this very clear I think, sorry for that.
I load the filenames into a datagridview:
Then I have two possible scenarios.
1. I click on a filename and make a preview of the report (in this case I show the form with my usercontrol)...It shows all data in textboxes no matter how many tabs are selected...but when I print I this preview of the report the textboxes are filled in only If the tab(second one) was actually clicked and shown. (shown is if only second tab is selected or if I click it and show it in the preview).

2. I iterate trough all rows in datagridview and print all filenames that have at least one checkbox(define which tab is filled) selected.
In this case I never get textboxes filled.

As I see it the problem is similar as the one I had that printed tabs were blank if I didn't show them, until I used your solution, just that this happens to textboxes only. Weird to me is that labels are normally filled and updated (text and color)

Regards,
grega
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Tags
print, silent, tabpage


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 5:53 AM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.