Windows Develop Bookmark and Share   
 index > Windows Forms General > Multiple Forms / Calling a Single Form from the Code
 

Multiple Forms / Calling a Single Form from the Code

Hi,

I am a bit new to C# - though I have managed to get some sample programs/projects to compile.

I would like to know how to get two or more forms to work. More specifically, I would like to know how to start up a single form from a function or something like that. (I tried to search for the terms “Multiple Forms�or on “Exchanging Data between multiple forms�and could not get any results.)

I first started out with a simple project, that works created in Visual Studio 2005 that also contained a form. Up to this part, the form displayed fine. Visual Studio had a class Program which had a main() function that would crank this entire thing. Now I declared a Main class (static class Main) �then I copied the main() method from the Program class to the Main class.

Code Block

[STAThread] static void main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new MainGUI());

//Main mn = new Main();

}


This does not work. It tells me that I cannot declare “Application�in this context. I did not understand why. I guess that the reason for my mistakes might be that I did not understand what exactly is [STAThread] �it was set automatically inside the “Program Class �and I manually moved it. I thought this was just a way to tell the code where to start execution so I did not think it would matter.

I would be glad if anyone had some ideas. Thanks to all you guys.

Regards,

O.O.

o-o-o  Sunday, December 23, 2007 4:25 AM

Hi, o-o-o,

1) I guess the reason why you cannot compile is that you missed this line in your own class:

Code Block

System;

System.Collections.Generic;

System.Linq;

System.Text;

System.Windows.Forms;

WindowsFormsApplication1

{

static class Main

{

[STAThread]

public static void main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}

}

http://msdn2.microsoft.com/en-us/library/ms157902.aspx

You can just use

Code Block

public static void main()

{

Form1 form = new Form1();

form.Show();

}

to show a new Form.

If you want to start the form in anotherprocess, you can do it like this

Code Block

public static void main()

{

System.Diagnostics.Process.Start(AppDomain.CurrentDomain.FriendlyName);

}

More info

http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.start.aspx

Hope this helps,

Regards

Yu Guo â€?MSFT  Tuesday, December 25, 2007 8:05 AM
Try this:

Main mn = new Main();
mn.Show();
blueparukia  Sunday, December 23, 2007 4:48 AM

Code Block

[STAThread]

public static void main()

{

Main mn = new Main();

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

mn.Show();

Application.Run();

}

The STAThread attribute, use it if you will be interacting with COM processes in a Single Threading Apartment model. Without it, you will be interacting with COM processes in the Multiple Threading Apartment model.

usually you don't need it , only required if you thing use your program with COM interoperability.

If my post resolve your question please don't forget mark the post as correctanswer.

Juan Carlos Ruiz [BogotaDotNet.org]  Sunday, December 23, 2007 5:44 AM
blueparukia wrote:
Try this:

Main mn = new Main();
mn.Show();


Dear blueparukia,

Thanks for your post. The .Show() method does not seem to work �but the .ShowDialog() method does. This form would be my base or main form so why does it need to be modal. I thought that it should be modeless �because ultimately I might want to spring up additional forms from it, but I have not yet got to that point as yet.

Any ideas?

O.O.


o-o-o  Sunday, December 23, 2007 8:59 AM
Juan Carlos Ruiz Pacheco wrote:

If my post resolve your question please don't forget mark the post as correctanswer.



Dear Juan,

Thanks for your post �but it does not resolve my issue. The issue above was the complaint from the compiler (inside Visual Studio 2005) that I could not declare “Application�in this context - when the context was static.

However, thanks for your explanation regarding the [STAThread]though. But regarding that, how does the Visual Studio figure out where to start execution from?? If [STAThread]has only meaning for interaction with the COM, and for Multi-Threaded Applications?

O.O.


o-o-o  Sunday, December 23, 2007 9:08 AM

Hi, o-o-o,

1) I guess the reason why you cannot compile is that you missed this line in your own class:

Code Block

System;

System.Collections.Generic;

System.Linq;

System.Text;

System.Windows.Forms;

WindowsFormsApplication1

{

static class Main

{

[STAThread]

public static void main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}

}

http://msdn2.microsoft.com/en-us/library/ms157902.aspx

You can just use

Code Block

public static void main()

{

Form1 form = new Form1();

form.Show();

}

to show a new Form.

If you want to start the form in anotherprocess, you can do it like this

Code Block

public static void main()

{

System.Diagnostics.Process.Start(AppDomain.CurrentDomain.FriendlyName);

}

More info

http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.start.aspx

Hope this helps,

Regards

Yu Guo â€?MSFT  Tuesday, December 25, 2007 8:05 AM

You can use google to search for other answers

Custom Search

More Threads

• How can I make my toolbar in MdiForm not transparent?
• Windows Form Border Problem with Themes
• Combo boxquestion
• How can I compare enums?
• RichTextBox row height
• open file dialog
• The WebBrowser control and Internet Explorer settings
• Multiline Textbox scrolling
• InvalidOperationException: Collection was modified
• Is a Form also a Thread ?