views:

244

answers:

4

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?

A: 

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?

Tony
This is what I opted for in the end - feels like a bit of a hack, but it works.
BENBUN Coder
Actually , GotFocus is called after the Shown event, however using a delay timer is a nice hack, can u post the solution when your done? i'm highly interested.
Madi D.
A: 

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
Madi D.
This looks likes a good approach, but unfortunately the event does not trigger when the form is shown. Code is as per http://codepaste.net/j6wdbg
BENBUN Coder
Add a Form.Focus = true on form load, i will edit my code to add that :)
Madi D.
The reason the GotFocus event doesn't fire is because the TextBox has the focus.
monkey_p
Correct me if i am wrong but if u force this.Focus() , wouldn't that force the Focus on the form, subsequently calling the gotfocus event ?
Madi D.
Also, the GotFocus will fire every time you change to that form. Example, minimize and restore will cause it to fire again.
monkey_p
Actually i just edited because i noticed it will keep firing, so i added a comment to remind him to change Focus at the end, however if he doesn't want the event to re-fire for minimizing or restore, they can add a variable to handle first Focus and set it true after the 1st time the event gets called.
Madi D.
A: 

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?

monkey_p
I tested the events using message boxes.. Gotfocus gets called after shown :)
Madi D.
In all my tests it fired before Shown, but I think it will depend on where the form gets focus.
monkey_p
A: 

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

Pondidum