简体   繁体   中英

Is there a way to have properties for a Winforms control that behave differently at design time (in VS designer)?

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?

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.

您可能可以通过在加载时触发的事件轻松删除那些锚点。

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; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM