views:

747

answers:

3

I have a namespace Company.UI.Forms where we have a form base class BaseForm that inherits from System.Windows.Forms.Form.

I need to limit the size of this form, so that if a concrete form, say ExampleForm, derives from BaseForm, it should have a fixed size in the VS designer view. The fixed size should be set or defined in some way (in the BaseForm ctor?) so that the derived ExampleForm and the designer picks it up.

Is that possible?

[Edit] The purpose is to use the base class as a template for a full screen form for a Windows CE device of known screen size, for which I want to be able to lay out child controls in the designer.

+1  A: 

I don't believe there is any direct support for this type of scenario. All of the values which control the maximum size on a Form can be overridden by a child form. Hence there is no way to constrain it from a property perspective.

The only way I can think of is to override the size changed method and reset the size to the max size if it ever goes over. This may have some bad effects though and you should expirement with it before you settle on it as a solution.

JaredPar
+1  A: 

In standard WinForms you can override the SetBoundsCore method. This code will prevent the base classes to change it at design time... but remember it can be overriden by any inherited class anyway:

protected override void SetBoundsCore(int x, int y,
     int width, int height, BoundsSpecified specified)
{
  if (this.DesignMode)
    base.SetBoundsCore(x, y, 500, 490, specified);
  else
    base.SetBoundsCore(x, y, width, height, specified);
}

I've put the if because you specified to be at design time, if you don't want a resize at runtime as well remove it.

jmservera
`SetBoundsCore` isn't available in .Net CF... :-(
Johann Gerell
oops, sorry, I didn't notice it was about CF
jmservera
See my other solution as alternative answer...
jmservera
+1  A: 

Ok, now for CF, quick solution, you may do a cleaner code by storing Size in a field...

public BaseForm()
{
 InitializeComponent();
 this.Size=new Size(20,20);
}


bool resizing;

protected override void OnResize(EventArgs e)
{   
 if(!resizing)
 {
   resizing = true;
   try
   {
     this.Size = new Size(20, 20);
   }
   finally
   {
     resizing = false;
   }
 }
 else
   base.OnResize(e);
}
jmservera
And the derived class can then just override OnResize and you've accomplished nothing.
ctacke
Yes, of course, but you usually do that to avoid to change the form's size by mistake using the designer, not to tie the hands of a developer. Please read the question where it asks: "it should have a fixed size in the VS designer view".
jmservera