AngryHacker
AngryHacker

Reputation: 61646

WinForm set to be Maximized shows up too early in the cycle

Code for the form:

public partial class Foo: Form
{
    public Foo()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        // Form already visible here when Maximized from calling code
        base.OnLoad(e);
    }
}

Calling code:

Foo foo = new Foo();
foo.WindowState = FormWindowState.Maximized;
foo.ShowDialog();

When the code enters the OnLoad event, the Foo form is already displayed on the screen. If I remove the foo.WindowState = FormWindowState.Maximized statement, then the Foo form is not visible in the OnLoad event (as it should be).

Why is it and what can I do to fix the issue? The issue being that when the form is set to Maximized, it shows up too early in the cycle.

Note that there is a similar question, but it focused on UI antics and didn't really address the problem.

Upvotes: 5

Views: 1979

Answers (1)

Igby Largeman
Igby Largeman

Reputation: 16747

This kind of problem usually warrants some careful thought about how you're doing things. A rethink on your strategy for loading, binding and displaying forms may be in order. However, for a simple solution, you could do this:

Foo foo = new Foo();
foo.Shown += (s, a) => foo.WindowState = FormWindowState.Maximized;            
foo.ShowDialog();

This way, you won't maximize the form until the Shown event is raised, which happens after OnLoad().

Upvotes: 5

Related Questions