?
Solved

Print current tabpage of tabcontrol

Posted on 2004-10-21
4
Medium Priority
?
1,463 Views
Last Modified: 2008-02-01
I have created a tabcontrol (TabControl1) in VB.NET with four tabpages.  I have put a button (Button1) on TabPage 2 and added a PrintDocument control.  I would like to know what code needs to be added under the Private Sub Button1_Click event to print TabPage2.  Thank you for your help.
0
Comment
Question by:georgecoppens
2 Comments
 
LVL 48

Accepted Solution

by:
Mikal613 earned 500 total points
ID: 12377239
0
 
LVL 16

Assisted Solution

by:jimbobmcgee
jimbobmcgee earned 500 total points
ID: 12401248
It's a real pain to print controls in a container.  Basically you have to define your own flexible printer functions for each control type, then wrap it in a For Each/Next loop to iterate through your controls.  Not nice, or easy...

Alternatively, you can copy an area of the screen to a bitmap, then attempt to print that, but that is also difficult:  http://www.andreavb.com/tip090001.html  may give you some ideas on this...

The nicest way to print WYSIWYG controls in an area is to use the PrintForm method of the Form.  However, you don't want the whole form, so I suggest, combining the the methods to get something you might like:

I will assume you have your tab-contents in a Frame (but it will work for a Picturebox, with a little tweaking), on a form called frmInput.  It will create controls on a dummy form called frmOutput, before calling frmOutput.PrintForm:

    Private Sub CommandButton1_Click()
   
        On Error Resume Next
   
        Dim ctl As Control
       
        frmOutput.Width = frmInput.Frame1.Width
        frmOutput.Height = frmInput.Frame1.Height
   
        For Each ctl In frmInput.Controls
       
            If TypeOf ctl Is TextBox Then
           
                Set prn = frmOutput.Controls.Add("VB.TextBox", , True)    'CREATE A TEXT BOX
               
                    prn.Top = ctl.Top
                    prn.Left = ctl.Left
                    prn.Height = ctl.Height
                    prn.Width = ctl.Width
                    prn.Text = ctl.Text
                    prn.Visible = ctl.Visible                                'SET IT TO MATCH frmInput TEXT BOX
                   
                Set prn = Nothing
               
            ElseIf TypeOf ctl Is ComboBox Then
           
                Set prn = frmOutput.Controls.Add("VB.ComboBox", , True)
               
                    prn.Top = ctl.Top
                    prn.Left = ctl.Left
                    prn.Height = ctl.Height
                    prn.Width = ctl.Width
                    prn.Text = ctl.Text
                    prn.Visible = ctl.Visible
                   
                Set prn = Nothing
               
            ElseIf TypeOf ctl Is OptionButton Then
           
                Set prn = frmOutput.Controls.Add("VB.OptionButton", , True)
               
                    prn.Top = ctl.Top
                    prn.Left = ctl.Left
                    prn.Height = ctl.Height
                    prn.Width = ctl.Width
                    prn.Caption = ctl.Caption
                    prn.Value = ctl.Value
                    prn.Visible = ctl.Visible
                   
                Set prn = Nothing
               
            ElseIf TypeOf ctl Is CheckBox Then
           
                Set prn = frmOutput.Controls.Add("VB.CheckBox", , True)
               
                    prn.Top = ctl.Top
                    prn.Left = ctl.Left
                    prn.Height = ctl.Height
                    prn.Width = ctl.Width
                    prn.Caption = ctl.Caption
                    prn.Value = ctl.Value
                    prn.Visible = ctl.Visible
                   
                Set prn = Nothing
           
            End If
       
        Next ctl
       
        frmOutput.Show False
        frmOutput.Repaint
        frmOutput.PrintForm
   
    End Sub

I haven't had a chance to test this; I expect the class strings need debugging ("VB.CheckBox", "VB.TextBox", etc).  I haven't found a comprehensive list of these...

Give it try and tweak as required -- you may get something good that works for you...

HTH

J.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month12 days, 23 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question