By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,187 Members | 1,629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,187 IT Pros & Developers. It's quick & easy.

Question about MainForm startup

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsapplicationclassruntopic.as p>

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/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsformclassformborderstyletop ic.asp>

<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsformborderstyleclasstopic.a sp>

Hope this helps...
Regards,
Madhu

MVP - C# | MCSD.NET

"Sam Sungshik Kong" wrote:
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

Nov 16 '05 #2

P: n/a
Thanks for the answer.
See in-line for additional questions.

"Madhu[C#-MVP]" <Ma*******@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.com...
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
Nov 16 '05 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com


Thanks.

Sam

Nov 16 '05 #4

P: n/a
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com


Thanks.

Sam


Nov 16 '05 #5

P: n/a
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" <ss*@chol.nospam.net> wrote in message
news:eb**************@TK2MSFTNGP11.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com
>
>
> Thanks.
>
> Sam
>
>



Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.