google rss rss

doneykoo [Ouditian]

DKzone- Ouditian Technology

Where is Form's Loaded event? [FW]

Where is Form's Loaded event?

[FW]http://weblogs.asp.net/kennykerr/archive/2004/11/26/where-is-form-s-loaded-event.aspx

Wow, it’s been a while since I last posted something here. We’ve recently moved to BC so I’ve been pretty distracted. I hope to be able to post more regularly in the coming weeks.

This is a bit off-topic for me but here goes.

Recently I needed to run some code right after a form is displayed for the first time. The Form.Load event is handy for performing various tasks when a form is loading but before it is displayed for the first time. Unfortunately there is no corresponding Form.Loaded event to notify the application that the form has actually loaded and is visible.

Fortunately it’s quite easy to pull it off without resorting to the WaitForInputIdle function. All you need to do is override Form’s OnLoad method and add an event handler for the Application.Idle event. Since we only want to be notified a single time that the form is loaded, we immediately remove the delegate in the event handler. You can of course register the event handler earlier in the form or application’s lifetime but I prefer to keep delegates registered for as short a period as possible.

Here’s a simple example:


 1 protected override void OnLoad(EventArgs args)
 2 {
 3     base.OnLoad(args);
 4 
 5     Application.Idle += new EventHandler(OnLoaded);
 6 }
 7 
 8 private void OnLoaded(object sender,
 9                       EventArgs args)
10 {
11     Application.Idle -= new EventHandler(OnLoaded);
12 
13     // TODO: add relevant code here
14 }
15 

This might be useful, for example, if you need to prompt the user (the horror!) for something but would prefer the dialog box to appear in the context of your application’s main window.


© 2004 Kenny Kerr

posted on 2009-10-16 09:48 DoNeY 阅读(279) 评论(0)  编辑  收藏 所属分类: C/C++


只有注册用户登录后才能发表评论。


网站导航:
 
  doneykoo blogjava