Question about MainForm startup

  • Thread starter Thread starter Sam Sungshik Kong
  • Start date Start date
S

Sam Sungshik Kong

Hello!

The normal way of Main for a Windows Forms application is like this.

Application.Run(new Form1());


If I change it like the following, is there any problem?

(new Form1()).ShowDialog();


TIA.
Sam
 
Hi Sam,

The Application.Run method is to use to start the application message loop.
You cannot give form1().ShowDialog there. Check the documentation for
Application.Run method in the link below,

<http://msdn.microsoft.com/library/d...ystemwindowsformsapplicationclassruntopic.asp>

I guess what you are trying to do is to show the form as a dialog. What you
should be doing is to set the FormBorderStyle property for the form. You have
various types of border style like FixedDialog, FixedSingle, Sizable etc. The
default is Sizable. check the below links for the information about
FormBorderStyle property and the enumeration.

<http://msdn.microsoft.com/library/d...windowsformsformclassformborderstyletopic.asp>

<http://msdn.microsoft.com/library/d...stemwindowsformsformborderstyleclasstopic.asp>

Hope this helps...


Regards,
Madhu

MVP - C# | MCSD.NET
 
Thanks for the answer.
See in-line for additional questions.

Madhu said:
Hi Sam,

The Application.Run method is to use to start the application message loop.
You cannot give form1().ShowDialog there.

Yes I can. It runs without any problem (No Application.Run Method call is
needed in that case). But I want to make sure that it does not involve any
touble.

I guess what you are trying to do is to show the form as a dialog.

What I am trying to do is just to understand what Application.Run does. I
know it manages the message pump. If (new Form1()).ShowDialog() runs ok
without Application.Run, then what manages message pump?


Thanks.

Sam
 
Sam,

See inline.
Yes I can. It runs without any problem (No Application.Run Method call is
needed in that case). But I want to make sure that it does not involve any
touble.

You can, but it's not the right way to do it. You should call
Application.Run to make sure the message loop is primed properly.
What I am trying to do is just to understand what Application.Run does. I
know it manages the message pump. If (new Form1()).ShowDialog() runs ok
without Application.Run, then what manages message pump?

When you show the form as a dialog, another pump internal to the dialog
handles the windows messages (same thing with messagebox, menus, etc, etc).
That's the pump that is being run. If another message that the dialog can't
handle comes in on that pump (and it might, since anything can come in
really), then it won't know what to do.

Hope this helps.
 
Thanks, Nicholas!

Actually I'm trying to prove that (new Form1()).ShowDialog() is a wrong way.
The best way would be to show the problems that the wrong way may cause.
You said that some messages will not be handled if Application.Run is not
used.
Can you show me some examples of such case?

Thank you.
Sam

Nicholas Paldino said:
Sam,

See inline.
Yes I can. It runs without any problem (No Application.Run Method call is
needed in that case). But I want to make sure that it does not involve any
touble.

You can, but it's not the right way to do it. You should call
Application.Run to make sure the message loop is primed properly.
What I am trying to do is just to understand what Application.Run does. I
know it manages the message pump. If (new Form1()).ShowDialog() runs ok
without Application.Run, then what manages message pump?

When you show the form as a dialog, another pump internal to the dialog
handles the windows messages (same thing with messagebox, menus, etc, etc).
That's the pump that is being run. If another message that the dialog can't
handle comes in on that pump (and it might, since anything can come in
really), then it won't know what to do.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Thanks.

Sam
 
Hi Sam,

IMHO it is totally correct to show dialog box like that. How Nicholas
explained there is internal message pump that will serve your dialog. I
disagree that there would be some unhandled messages. Everything is caught
and dispatched to the form. If your form's ignore some that's fine;
otherwise I believe everything will run smoothly.


--
Stoitcho Goutsev (100) [C# MVP]

Sam Sungshik Kong said:
Thanks, Nicholas!

Actually I'm trying to prove that (new Form1()).ShowDialog() is a wrong
way.
The best way would be to show the problems that the wrong way may cause.
You said that some messages will not be handled if Application.Run is not
used.
Can you show me some examples of such case?

Thank you.
Sam

in
message news:%[email protected]...
Sam,

See inline.
The Application.Run method is to use to start the application message
loop.
You cannot give form1().ShowDialog there.

Yes I can. It runs without any problem (No Application.Run Method call is
needed in that case). But I want to make sure that it does not involve any
touble.

You can, but it's not the right way to do it. You should call
Application.Run to make sure the message loop is primed properly.
I guess what you are trying to do is to show the form as a dialog.

What I am trying to do is just to understand what Application.Run does. I
know it manages the message pump. If (new Form1()).ShowDialog() runs ok
without Application.Run, then what manages message pump?

When you show the form as a dialog, another pump internal to the dialog
handles the windows messages (same thing with messagebox, menus, etc, etc).
That's the pump that is being run. If another message that the dialog can't
handle comes in on that pump (and it might, since anything can come in
really), then it won't know what to do.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Thanks.

Sam
 
Back
Top