tags:

views:

35

answers:

3

I am looking for a way for some controls to have Anchor = Top, Bottom, Left, Right at design time so they would change when I scale the parent form. But doesn't do this at runtime when the parent form is scaled.

Is there something like this?

+2  A: 

You can probably easily remove those anchors with an event that fires on load.

StrixVaria
+3  A: 

You can use the DesignMode property to detect whether the control is currently in design mode or not. That way you should be able to set an appropriate value in the Anchor property (and other properties as well) to behave as you wish in design- and non-design modes.

Fredrik Mörk
+1  A: 

This sample control works like that:

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

internal class SampleControl : Control {
    public SampleControl() {
        this.BackColor = Color.Yellow;
    }
    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        if (this.DesignMode) this.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
    }
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override AnchorStyles Anchor {
        get { return base.Anchor; }
        set { base.Anchor = value; }
    }
}
Hans Passant
Hi nobugz, haven't seen you for a long time, since msdn.
Joan Venge