Print current tabpage of tabcontrol

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.
georgecoppensAsked:
Who is Participating?
 
jimbobmcgeeConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.