Inherited form control anchoring

G

Guest

Hi,

I'm using VS2005 Pro and trying to minimise the redundant code between a
number of similar forms in a C# project.

I’ve produced a base form, which contains a few buttons, a graphic and some
associated code. This works okay programmatically, but I find that the
buttons (which are anchored to the right and / or bottom of the form) are not
being placed correctly when first drawn if the inherited form’s initial size
is different to the base form’s. For subsequent changes in form shape within
the designer or a running application, the behaviour with regard to anchoring
is as expected (i.e. the gap between button and bottom border remains
constant – it’s just not what I originally specified).

It seems the buttons are being drawn in their original position (relative to
the top left of the form) and *then* anchored to the form’s bottom / right
borders, regardless of whether the border is also in its original position.
As the form is inherited, the buttons cannot be manually realigned in the
designer.

If anyone can tell me how to solve (or perhaps get around) this problem,
that would be great! It would be quite untidy to have to have all the forms
the same dimensions or anchor everything to the top left.

Cheers,
Mike
 
M

Marius Groenendijk

Hello Mike,

This is clearly a bug. I know it happens in VS 2003 (VB), thought it would
be solved
in VS 2005. Apparently it hasn't.

If you comment out 'this.SuspendLayout()' in InitializeComponent in the base
form
it *might* work as expected but as soon as you modify the designer the
problem
crops up again.

Our workaround is to 'fix' the layout problems in the OnLoad override of the
base
form. If it's only for a few controls that's doable.

protected virtual void OnLayoutHack()
{
// TODO: fix the layout problems
// For ex. position OK, Cancel and Help buttons where they ought to be.
}

protected override void OnLoad(EventArgs e)
{
OnLayoutHack();
base.OnLoad (e);
}

In derived forms you might want to do something else w.r.t. layout, hence
virtual OnLayoutHack.

Once OnLayoutHack has done what *you* want the anchoring behaves as
expected.

HTH & Kind regards,
Marius.
 
G

Guest

Marius,

Thanks very much for your response – just what I needed. It’s only for 3-4
controls, so I’ll implement your OnLoad solution. I like your trick of moving
the actual fix into a virtual function, too – it makes the whole system more
flexible.

I couldn’t see any reason for behaviour like this to occur ‘by design’
either; shame it hasn’t been fixed between VS versions!

Cheers,
Mike
 
G

Guest

Marius,

Thanks very much for your response – just what I needed. It’s only for 3-4
controls, so I’ll implement your OnLoad solution. I like your trick of moving
the actual fix into a virtual function, too – it makes the whole system more
flexible.

I couldn’t see any reason for behaviour like this to occur ‘by design’
either; shame it hasn’t been fixed between VS versions!

Cheers,
Mike
 

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