Events on Form

  • Thread starter Thread starter Lars Black
  • Start date Start date
L

Lars Black

I'm a bit puzzled about the way events are triggered on a form. Given the
code below, there seems to be a difference between using Show and
ShowDialog.

Form2 frm;
// Test ShowDialog
frm = new Form2();
frm.ShowDialog();
frm.Visible = false;
frm.ShowDialog();
frm.Dispose();

// Test Show
frm = new Form2();
frm.Show();
frm.Visible = false;
frm.Show();

I've put logging in all the eventhandlers (not a messagebox that removes
focus), and here's the result:
ShowDialog:
Activated
GotFocus
Load
Deactivated
LostFocus
Load <==== **** Why this one??
Activated
GotFocus

Show:
Activated
GotFocus
Load
Deactivated
LostFocus
Activated
GotFocus
LostFocus

Why is Load called twice in the ShowDialog example? The help says "Occurs
before a form is displayed for the first time", but it obvious also fires
when the form is show the second time...

Is this a bug?

Cheers,
Lars
 
ShowModal displays a form awaits for user interaction by placing the main
thread in the child forms message pump.

Show simply displays the form, but button presses etc are not effective, and
the main thread carries on running...

in relation to your example for a basic default form:

// snippet for ShowModal
frm = new Form2();
frm.ShowModal (); // <-- frm is displayed
frm.Dispose(); // <-- not called until the user exits from Form2

// snippet for Show
frm = new Form2();
frm.Show (); // <-- the form is made visible
frm.Hide (); // <-- this is called right after Show to hide the form
again
frm.Dispose();


You should only call ShowDialog on a form object once. If you need to
redisplay a form, it should be in the of a new instace of the form.
Note the example of the MSDN, the method can be called multiple times, and
each time a "new" object is created:

public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();

// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
testDialog.Dispose();
}
 
ShowModal displays ... Show simply displays ...

I knew that much..
You should only call ShowDialog on a form object once. If you need to
redisplay a form, it should be in the of a new instace of the form.

but this is new to me! Is that documented anywhhere?
I have an order handling form with a lot of controls. I create it at startup
and when needed, I set a few properties on it and calls ShowDialog to handle
the order entry. To avoid the quite lengthy create-time, I reuse it from
time to time.
It works basically without problems, except that I have put code in the Load
event that only was supposed to be run once for the form - that's why I
wonder about the multiple Load events...

Cheers,
Lars
 
I've just googled about this issue and found an interesting post from Katie
Schaeffer [MSFT]
http://groups.google.com/[email protected]&rnum=1

"Dialogs are also meant to be re-usable, allowing you to call .ShowDialog
multiple times after the dialog has been closed "

So I don't think your explanation is right - and I'm still trying to figure
out why there's an extra Load event when I call ShowDialog :-)

Cheers,
Lars
 
The method Control.Show calls Control.SetVisibleCore method that checks
for window.Handle initialization. If initialization has not been
implemented, it would call Control.CreateControl (initialization,
Form.OnCreateControl call, Form.OnLoad). That's why you see only one
OnLoad event.

For the Form.ShowDialog the situation is completely different. The
handle creates and destroys for every call that's why OnLoad calls for
every ShowDialog call:

try
{
...
Visible = true; // at this moment a handle was not created
// init, OnCreate, OnLoad will be called.
Application.RunDialog(this);
}
finally
{
if (base.IsHandleCreated)
{
this.DestroyHandle();
}
}

Hope this help,
Sergey Bogdanov
 
Hope this help,

It certainly did - thanks for a good explanation!
I hope Microsoft reads it and put it in the helpfile somewhere :-)

Cheers,
Lars
 
Back
Top