Question Print, Print Preview, and PageSetup Dialog for TabPage in TabControl

nlraley

Member
Joined
Jul 15, 2010
Messages
7
Programming Experience
Beginner
I have a tab control with 2 tab pages contained within it that are each a seperate windows form.

I am tyring to use the following dialogs indicated above to print the contents of the windows form shown in the tab page of the tab control; however, I can't quite seem to figure out how to get the contents of the tab page to display. I am just getting blank pages on my print preview.

Any ideas how to set the document for the dialog to the tab page of a tab control? Nothing that I have tried will work and using Me, or this b/c I am using at the momment, yields a blank page. So if anyone can offer a recommendation or point me in the right direction I'd apprecaite it.

Thanks
 
Any ideas how to set the document for the dialog to the tab page of a tab control?

You don't. The only thing you can assign to those Document properties is a PrintDocument. The idea is that you create a PrintDocument and handle its PrintPage event, using GDI+ to draw the printed page. In your case, you would draw something different depending on which tab was selected. You assign the PrintDocument to the Document property of those dialogues and then any changes you make in the dialogues are propagated to the PrintDocument, affecting what you draw in the PrintPage event handler. Here's a very simple example of what your PrintPage event handler might look like:
VB.NET:
Dim message As String = Nothing

Select Case Me.TabControl1.SelectedIndex
    Case 1
        message = "Hello World"
    Case 2
        message = "Goodbye Cruel World"
End Select

e.Graphics.DrawString(message, Me.Font, Brushes.Black, Point.Empty)
 
I understand that much, I guess I wasn't as clear.

Here's what I have, mind you it's c# atm but same principle follows in .net and I'm much more familiar with .net so I can easily convert a resolution to c#.

On my MainProject form that has a tab control with 2 tabs, their contents defined as follows:
VB.NET:
        MainForm fileManagementForm = new MainForm();
        UserManagement userManagementForm = new UserManagement();

        userManagementForm.TopLevel = false;
        userManagementForm.Parent = tabControl1.TabPages[0];
        userManagementForm.FormBorderStyle = FormBorderStyle.None;
        userManagementForm.Dock = DockStyle.Fill;
            
        userManagementForm.Show();

        fileManagementForm.TopLevel = false;
        fileManagementForm.Parent = tabControl1.TabPages[1];
        fileManagementForm.FormBorderStyle = FormBorderStyle.None;
        fileManagementForm.Dock = DockStyle.Fill;

        fileManagementForm.Show();

So the Main program form now has a tab control with 2 tabs, one tab is my user management form and the other the file management form.

On the main program I have declared the following:
VB.NET:
private System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();


Then for the user clicking the menu item for print preview I had tried doing what you mentioned. Like:
VB.NET:
printPreviewDialog1.Document = docToPrint;
printPreviewDialog1.Show();

However, my print preview is totally empty. How do I set the contents of my print document to that of the tab control tab page that is being displayed? Is it b/c I'm not calling the equivalent of e.drawing?
 
There is no e.drawing. As I said, to print something you must handle the PrintPage event of your PrintDocument and then use GDI+ to draw whatever it is that you want printed. Are you handling the PrintPage event? I don't see any code that indicates that you are. Whatever you draw in the PrintPage event handler is what will get printed. If you're not drawing anything then nothing will get printed.
 
I should have known it was handled in the Print event. I hand't worked with Printing in a while and was thinking that print preview didn't call the print event.

I have it up and working. For those wanting to know how here is what I did...

First create a CaptureScreen method to draw the contents of my form
VB.NET:
private void CaptureScreen()
        {
            Graphics mygraphics = this.CreateGraphics();
            Size s = this.Size;
            memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            IntPtr dc1 = mygraphics.GetHdc();
            IntPtr dc2 = memoryGraphics.GetHdc();
            BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
            mygraphics.ReleaseHdc(dc1);
            memoryGraphics.ReleaseHdc(dc2);
        }

Now when the user selects the print preview menu item I do the following:
VB.NET:
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            docToPrint.DocumentName = this.tabControl1.SelectedTab.Name;
            CaptureScreen();
            printPreviewDialog1.Show();
            
        }

Then finally I have this tidbit on the Print method for the document:
VB.NET:
private void docToPrint_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 0, 0);
        }

Rest of the functions are handled pretty much the same. Thanks for the help!
 
There are very few situations where printing an actual image of the form is the best option. If that's what you want to do though, there's a PrintForm component included in the VB PowerPacks, which is installed as standard with VB 2010. It's in the Toolbox by default.
 
I couldn't use the print form component b/c I was developing the app around some dll's used in 2.0 and whenever I added that component it would break those dll's.
 
I understand that much, I guess I wasn't as clear.

Here's what I have, mind you it's c# atm but same principle follows in .net and I'm much more familiar with .net so I can easily convert a resolution to c#.
Mind you, this is a VB.Net community, so you should have the courtesy of using VB.Net language when posting code here. If you need to convert the code to VB.Net that is your responsibility, not all VB.Net users here.
 
Sorry, in my defense my original post was theory on how to do it as I normally program in vb and this form is a great resource for help, I just posted the c# to show jm what I was working with.

Anyways, for those who can't figure it out here is the code more or less in vb with what I have thus far.

VB.NET:
Private Sub CaptureScreen()
	Dim mygraphics As Graphics = Me.CreateGraphics()
	Dim s As Size = Me.Size
	memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
	Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
	Dim dc1 As IntPtr = mygraphics.GetHdc()
	Dim dc2 As IntPtr = memoryGraphics.GetHdc()
	BitBlt(dc2, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height, dc1, _
		0, 0, 13369376)
	mygraphics.ReleaseHdc(dc1)
	memoryGraphics.ReleaseHdc(dc2)
End Sub

VB.NET:
Private Sub printPreviewToolStripMenuItem_Click(sender As Object, e As EventArgs)
	docToPrint.DocumentName = Me.tabControl1.SelectedTab.Name
	CaptureScreen()
	printPreviewDialog1.Show()

End Sub

VB.NET:
Private Sub docToPrint_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs)
	e.Graphics.DrawImage(memoryImage, 0, 0)
End Sub
 
Back
Top