BENBUN Coder
BENBUN Coder

Reputation: 4881

Triggering an event after a Winform layout is complete

I am working on a C# WinForm application.

I want to trigger some processing once the form has been "shown" and the layout of the form is complete.

I am using the "_Shown" event, but this seems to trigger before the layout of the form has completed. Is there event I can use that fires once the layout is complete?

Upvotes: 10

Views: 18755

Answers (8)

Jean-Claude Lanz
Jean-Claude Lanz

Reputation: 1

Try this:

private void Form1_Load(object sender, EventArgs e)
{
    Application.Idle += Form1_Loaded;
}

private void Form1_Loaded(object sender, EventArgs e)
{
    Application.Idle -= Form1_Loaded;
    // do the stuf
}

Upvotes: 0

znelson
znelson

Reputation: 924

This works for me and is much less "hacky" than other suggestions:

protected override void OnLayout(LayoutEventArgs levent)
{
    base.OnLayout(levent);

     if(someControl == null)
       return; // be careful of OnLayout being called multiple times

    // otherwise, do some stuff here, set control sizes, etc.
}

Upvotes: 2

JS5
JS5

Reputation: 779

The best solution is the Shown() event: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.shown.aspx

"The Shown event is only raised the first time a form is displayed; subsequently minimizing, maximizing, restoring, hiding, showing, or invalidating and repainting will not raise this event."

Upvotes: 1

MikeM
MikeM

Reputation: 231

Put Application.DoEvents() at the start of the form's Shown event handler. This will force all the controls to be rendered.

Upvotes: 23

Madi D.
Madi D.

Reputation: 1990

Try using Form.GotFocus (inherited from control).. something like this.

   private void Form1_Load(object sender, EventArgs e)
    {
        this.GotFocus += new EventHandler(Form1_gotFocus);
        this.Focus();
    }

    private void Form1_gotFocus(object sender, EventArgs e)
    {
      // You will need to Switch focus from form at the end of this function, 
      //to make sure it doesnt keep Firing.
    }

According To msdn , the following happens:

When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl..::.ActiveControl property to the current form, focus events occur in the following order:

  1. Enter
  2. GotFocus
  3. Leave
  4. Validating
  5. Validated
  6. LostFocus

Upvotes: 0

Pondidum
Pondidum

Reputation: 11637

An old trick in VB6 used to be to use the Paint event:

bool firstShown = false;

void form_Paint(Object sender, EventArgs e) {
  if ( !firstShown ) {
    YourMethodThatNeedsToRunOnShown();
    firstShown = true;
  }

  //the rest of your paint method (if any)

}

It is a little hacky, but it does work

Upvotes: 3

monkey_p
monkey_p

Reputation: 2879

AS far as I can remember the event order is something like

Form.Load
Form.Layout 
Form.VisibleChanged
Form.GotFocus
Form.Activated
Form.Shown

So if something is still happening after Form.Show it's because of the way you coded it.

Are you maybe creating the form dynamically?

Upvotes: 1

Tony The Lion
Tony The Lion

Reputation: 63260

I don't see an event after Shown you can use for this purpose. Could you not use a timer to delay your processing in the Shown event?

Upvotes: 6

Related Questions