Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Inheriting form with TabControl
 

Inheriting form with TabControl

I have a weird problem with the Designer when it’s a inherited form.

I have a Form (form2) with is inheriting another Form (form1), in which form1 contains a TabControl. The TabControl has 2 TabPages. And the TabPage Modifier is set to Public. When I add a Control to form2 in the TabPage with the Anchor Property set to “Top, Bottom, Left, Right� The size of form2 is not the same as form1, when you close and open the form again in Design mode the Control is not displayed in the correct location.


Thanks in advance.


Jason deBono
Jason deBono  Tuesday, April 21, 2009 9:59 AM
Hi Jason,

I performed a test based on your description but didn't reproduce the problem on my side. When I add a Panel in the first TabPage within the TabControl on the Form2 and set the Anchor property of the Panel to "top, buttom, left,right", the size of the Form2 doesn't change at all.

I close the Form2 and open it again. The location of the Panel in the first TabPage doesn't change, either.

I'm using Visual Studio 2008. What's the version of Visual Studio you're using?

Sincerely,
Linda Liu
Linda Liu  Tuesday, April 28, 2009 4:08 AM

Hi Linda,

Thanks for your Reply.
The Problem is due to the form2 is inheriting form1 which has the TabControl.
This is the Designer’s Code.

Open form2 and try to fix the size of the Picturebox in TabPage2.

FORM 1

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.TabControl1 = New System.Windows.Forms.TabControl
        Me.TabPage1 = New System.Windows.Forms.TabPage
        Me.TabPage2 = New System.Windows.Forms.TabPage
        Me.TabControl1.SuspendLayout()
        Me.SuspendLayout()
        '
        'TabControl1
        '
        Me.TabControl1.Controls.Add(Me.TabPage1)
        Me.TabControl1.Controls.Add(Me.TabPage2)
        Me.TabControl1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.TabControl1.Location = New System.Drawing.Point(0, 0)
        Me.TabControl1.Name = "TabControl1"
        Me.TabControl1.SelectedIndex = 0
        Me.TabControl1.Size = New System.Drawing.Size(389, 318)
        Me.TabControl1.TabIndex = 0
        '
        'TabPage1
        '
        Me.TabPage1.Location = New System.Drawing.Point(4, 22)
        Me.TabPage1.Name = "TabPage1"
        Me.TabPage1.Padding = New System.Windows.Forms.Padding(3)
        Me.TabPage1.Size = New System.Drawing.Size(381, 292)
        Me.TabPage1.TabIndex = 0
        Me.TabPage1.Text = "TabPage1"
        Me.TabPage1.UseVisualStyleBackColor = True
        '
        'TabPage2
        '
        Me.TabPage2.Location = New System.Drawing.Point(4, 22)
        Me.TabPage2.Name = "TabPage2"
        Me.TabPage2.Padding = New System.Windows.Forms.Padding(3)
        Me.TabPage2.Size = New System.Drawing.Size(381, 292)
        Me.TabPage2.TabIndex = 1
        Me.TabPage2.Text = "TabPage2"
        Me.TabPage2.UseVisualStyleBackColor = True
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(389, 318)
        Me.Controls.Add(Me.TabControl1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.TabControl1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Public WithEvents TabPage2 As System.Windows.Forms.TabPage

End Class


FORM 2

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form2
    Inherits WindowsApplication1.Form1

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.TabPage2.SuspendLayout()
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'TabPage2
        '
        Me.TabPage2.Controls.Add(Me.PictureBox1)
        Me.TabPage2.Size = New System.Drawing.Size(520, 335)
        '
        'PictureBox1
        '
        Me.PictureBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.PictureBox1.BackColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(192, Byte), Integer), CType(CType(0, Byte), Integer))
        Me.PictureBox1.Location = New System.Drawing.Point(8, 170)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(643, 200)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Form2
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.ClientSize = New System.Drawing.Size(528, 361)
        Me.Name = "Form2"
        Me.TabPage2.ResumeLayout(False)
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox

End Class


Open form2 and try to fix the picturebox its will not keep it size and location.
I'm using Visual Studio 2008.

Thanks
Regards
Jason
Jason deBono
Jason deBono  Tuesday, April 28, 2009 6:39 AM

Hi Jason,

Thank you for your reply!

I performed a test based on your sample code but still couldn't reproduce the problem on my side.

The following are the steps of my test.
1. Create a WinForm application project. Copy the sample code in the InitializeComponent method in the Form1 you provided into the Form1 ofmy project.
2. Add an inherited form from the Form1 in my project. The name of the inherited form is called Form2.
3. Add a PicureBox onto the TabPage2 in the Form2. The following is the code in the InitializeComponent method in the Form2:

'Form2
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.TabPage2.SuspendLayout()
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'TabPage2
'
Me.TabPage2.Controls.Add(Me.PictureBox1)
'
'PictureBox1
'
Me.PictureBox1.BackColor = System.Drawing.Color.Maroon
Me.PictureBox1.Location = New System.Drawing.Point(8, 6)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(365, 278)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Form2
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.ClientSize = New System.Drawing.Size(389, 318)
Me.Name = "Form2"
Me.TabPage2.ResumeLayout(False)
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox

4. Close the Form2 and reopen it.
5. Open the Form2.Designer.vb file and I see nothing changed in this designer.vb file.

Please perform a test based on the above steps on your side to see if you get the same result.

Thanks,
Linda Liu

Linda Liu  Thursday, April 30, 2009 4:26 AM
Hi Linda,

I looked at the InitializeComponent method in the Form2.
Now just try to anchor the Picturebox (Important withALL sides) and Resize form2, close the Designer and Open it again.
By doing so it should write in InitializeComponent the following lines.
Me.PictureBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)

And also the new Size of the Form2

Close the Designer and Open it again. The Code in the Designer will remain the same, but the control will not be display in the same location, because of the anchoring.

Thanks

Jason deBono
Jason deBono  Thursday, April 30, 2009 6:57 AM

Hi Jason,

I perform some more tests on this issue and did reproduce the problem on my side. It seems that the form designer mis-resizes the PictureBox on the Form2 when the Form2 is re-opened.

I haven't found a solution or workaround to this issue. You may submita bug report in the Microsoft Connect web site on this issue:
http://connect.microsoft.com/visualstudio/feedback

Thanks,
Linda Liu

Linda Liu  Monday, May 04, 2009 7:31 AM

You can use google to search for other answers

Custom Search

More Threads

• Design view slow to load
• Visual Studio 2005 View Form Designer
• Multiple Forms
• Capture Enter key
• Why is MyForm derived from Form?
• Execute subroutine on Windows Form from a User Control on that form
• How to create usercontrol with combobox
• hyperlink to open a new form
• How does the Designer link the Name property to the control variable name?
• doubt in c#