Solved

Print current tabpage of tabcontrol

Posted on 2004-10-21
1,314 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
Question by:georgecoppens
    2 Comments
     
    LVL 48

    Accepted Solution

    by:
    0
     
    LVL 16

    Assisted Solution

    by:jimbobmcgee
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    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…

    933 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now