Dragonalighted
Dragonalighted

Reputation: 365

Panel doesn't resize it's contents in Design Mode

I have a UserControl I'm making which works fine when in execution, but doesn't resize in design mode.

The UserControl has an image background that is resized to fill when the Control resizes. Drawing that works fine. To deal with the fact that the image stretches, I have a panel that holds the content. During initialization, I store a Left, Top, Width and Height Ratio. ( pnl.Left / this.Width , etc..)

During Resize event, I then move and resize the content panel based on the new size of the control and the ratios I have saved. The _contentDimensions is a structure that stores the Ratios. ReDimension is a method that takes a control and moves/resizes it based on the ratio to new size.

private void Control_Resize(object sender, EventArgs e)
{
    try
    {
        this.SuspendLayout();
        _contentDimensions.ReDimension(this.ClientSize, pnlContent);
    }
    catch { }
    finally
    {
        this.ResumeLayout(true);
    }
}

While this works during runtime, If I resize the UserControl during DesignTime, The content Panel doesn't Resize.

Any Help would be appreciated. Thanks in advance.

Upvotes: 1

Views: 1634

Answers (2)

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

You are likely calling Control_Resize from the wrong location. Put your re-size logic in a overload of OnSizeChanged

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        MessageBox.Show("Size Changed!");
    }
}

When I add that control to my test form and attempt to re-size it I get this in visual studio:

enter image description here


As a extra side tidbit, calling this.DesignMode from your user control can tell you if you are being rendered in visual studio. This can be useful if you have some slow operations that happen on load (like starting a query to a database on a background thread) that you don't need to do if you are in design mode.

Upvotes: 1

Steve Wong
Steve Wong

Reputation: 2256

The Visual Studio Designer does not fire events for your UserControls. It works by constructing an instance of your object and parsing the InitializeComponent() method.

One possible workaround is to use the Anchor/Dock properties of a control to automatically size your child image and panel. See: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.anchor.aspx.

Upvotes: 0

Related Questions