Helpful Information
 
 
Category: C# Only
C# Only --> main() for Window Application/Form??

I noticed the main() is only present for the command line project. Is it possible to have a main() for the windows application project?

I prefer to have the main() that start up the whole thing because the code I'm using can check the license subscription in the main() before starting the windows forms.

Can this be done? I don't see any main() for hte windows application nor can I find one but I saw the "Program.cs" file. Is this file the one we're suppose to customize?



namespace WindowsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

Yes, that's the entrypoint for a WinForms project: the static Main() in the Program class.

Thanks... I have done C/C++. I haven't really got to do much with C# until now. A few people here and me at my company had decided to switch to C# and convert some VB projects to C# along the way. I'm very excited about it. :)

I noticed the main() is only present for the command line project. Is it possible to have a main() for the windows application project?

I prefer to have the main() that start up the whole thing because the code I'm using can check the license subscription in the main() before starting the windows forms.

Can this be done?
Yes.

A windows applications uses "Form_Load" in place of main() as its entry point. Put your startup logic in your startup form's "Load" event:

private void Form1_Load(object sender, EventArgs e)
{
// startup logic
}


Update: Oops. I was wrong. Ignore everything. Nothing to see here, carry on...

A windows applications uses "Form_Load" in place of main() as its entry point. Put your startup logic in your startup form's "Load" event:Holy crap. Does anybody read previous comments before they post?

The Form_Load event is not the entry point. VS2003 by default creates a static void Main() in the code of the first default form. VS2005 creates Program.cs and puts the static void Main() in a static class called Program.

If you do not have a static Main() entrypoint, you will receive a compile error.

Besides, the Form's static constructor and instance constructors run long before its Load event is fired.










privacy (GDPR)