Early this week a couple of my colleagues and I had a little discussion about events on the System.Windows.Forms.Form class. We were discussing if the Form had an event that would only be raised the first time the form was shown and never again. Personally I believed that the Form.Load event was doing just that, but after that discussion I was in great doubt so I decided to investigate it a little further.
I started by defining a list of the events that I thought could be interesting in the test:
Now for the test. My first test was simply to call the Show and Hide methods on the form. Given the results I thought that both my initial assumption and the assumption of one of my colleagues were correct: The Load and the Shown event was only raised once.
20070927 21:20 - Deactivate
----------------------------------------
20070927 21:20 - Activated
20070927 21:20 - GotFocus
----------------------------------------
20070927 21:19 - LostFocus
20070927 21:19 - Deactivate
----------------------------------------
20070927 21:19 - Shown
20070927 21:19 - Activated
20070927 21:19 - GotFocus
20070927 21:19 - Load
20070927 21:19 - HandleCreated
However after the next test I realized that I was wrong. This test was about calling the ShowDialog method and then use the forms "X" button to close it again.
20070927 21:28 - LostFocus
20070927 21:28 - Deactivate
20070927 21:28 - FormClosed
20070927 21:28 - Closed
20070927 21:28 - FormClosing
20070927 21:28 - Closing
20070927 21:28 - Shown
20070927 21:28 - Activated
20070927 21:28 - GotFocus
20070927 21:28 - Load
20070927 21:28 - HandleCreated
----------------------------------------
20070927 21:28 - HandleDestroyed
20070927 21:28 - LostFocus
20070927 21:28 - Deactivate
20070927 21:28 - FormClosed
20070927 21:28 - Closed
20070927 21:28 - FormClosing
20070927 21:28 - Closing
20070927 21:28 - Shown
20070927 21:28 - Activated
20070927 21:28 - GotFocus
20070927 21:28 - Load
20070927 21:28 - HandleCreated
What surprises me here is that the HandleCreated and HandleDestroyed is called every time you call the ShowDialog method and when the form closes again, but the Handle stayed the same while I ran the test.
The solution must be to create your own event that uses a boolean to indicate whether or not the form has been shown.
Thursday, September 27, 2007
Form event that is only raised the first time the form is shown
20070927 21:20 - LostFocus
20070927 21:28 - HandleDestroyed
Posted by
Robin Theilade
at
21:07
Labels: .net, events, programming, shown, system.windows.forms.form
Subscribe to:
Post Comments (Atom)


1 comments:
Hello! I'm newbie in Internet, can you give me some useful links? I know only about Yahoo [url=http://yahoo.com]Yahoo[/url] http://yahoo.com Yahoo
Post a Comment