Visual Inheritance Using Winforms

J

Jason Hickey

Has there been a change in the way the UI designer handles winform
inheritance in the 2003 version of visual studio.

Consider the following (try it if you are using 2003 Everything seems to
work under 2002)
Create a form and add two buttons (bottom right) and a panel (fill the top
of the form)
Anchor the buttons to the bottom right and anchor the panel to top bottom
left and right.
set all three controls access level to protected

Now when you resize this for the buttons stay at the bottom right and the
panel changes as you change the form size.
Compile...

Add a new inherited form to the project and select the form you just
created.
Everything looks good. Resize the form and see that the controls move
correctly.
Now re-compile the code... Now if yours behaves the same as mine the three
controls will move and jumble.

I tested on a first release of VS.Net and it seems to work fine. 2003
however is giving me fits.
We tested on several installs of 2003 and get the same result.

Is this just a bug in the designer? If so does anyone know of a workaround
or a fix.

I've included some code below check the difereneces between Form2.cs
(initial) and Form2.cs (After a move)
Note the addition of the following lines by the IDE:
this.panel1.Size = new System.Drawing.Size(372, 194);
this.button1.Location = new System.Drawing.Point(224, 210);
this.button2.Location = new System.Drawing.Point(304, 210);

The interesting thing is if you delete these lines everything will revert to
nomal (for the size of the current form).
In theory that is a work-around but you have to "re-delete" the lines
everytime you make a visual change to the form.
This would get VERY OLD VERY FAST if you have several levels of inheritance
as I do.

Thanks in advance...
-Jason


******************************* Form1.cs (this is the base class)
public class Form1 : System.Windows.Forms.Form
{
protected System.Windows.Forms.Panel panel1;
protected System.Windows.Forms.Button button1;
protected System.Windows.Forms.Button button2;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// panel1
//
this.panel1.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.To
p | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.panel1.Location = new System.Drawing.Point(8, 8);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(372, 164);
this.panel1.TabIndex = 0;
//
// button1
//
this.button1.Anchor =
((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bott
om | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(224, 180);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
//
// button2
//
this.button2.Anchor =
((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bott
om | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(304, 180);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "button2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(384, 213);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion
}

**********************************Form2.cs (After Initial Load
InitializeComponent() has 1 line
public class Form2 : Inh.Form1
{
private System.ComponentModel.IContainer components = null;

public Form2()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Designer generated code

private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}


**********************************Form2.cs (resize and compile
InitializeComponent() has size object for all items

public class Form2 : Inh.Form1
{
private System.ComponentModel.IContainer components = null;

public Form2()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Designer generated code
private void InitializeComponent()
{
//
// panel1
//
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(372, 194);
//
// button1
//
this.button1.Location = new System.Drawing.Point(224, 210);
this.button1.Name = "button1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(304, 210);
this.button2.Name = "button2";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(384, 243);
this.Name = "Form2";

}
#endregion
}
 
T

Tian Min Huang

Hi Jason,

Yan-Hong replied your post in microsoft.public.vsnet.general. I am pasting
his reply for your convenience:

"Hello Jason,

This is a known product issue and currently we are working with dev team
for a fix for it. Please refer to this post thread for more information.
Thanks.

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=GRDnvLIP
DHA.1640%40cpmsftngxa06.phx.gbl&rnum=1&prev=/groups%3Fq%3Dyhhuang%2Banchor%2
6hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3DGRDnvLIPDHA.1640%2540cpm
sftngxa06.phx.gbl%26rnum%3D1
"

Have a nice day!

Regards,
HuangTM
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top