Events on Form

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
 
D

Dan Bass

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();
}
 
L

Lars Black

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
 
L

Lars Black

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
 
S

Sergey Bogdanov

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
 
L

Lars Black

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top