views:

59

answers:

5

If I want to have multiple forms in an appication, can I do it with only one main? I am working with Visual Studio C# (Windows Application).

Thank you.

+2  A: 

Sure. There is nothing preventing you from having multiple Form instances in a .Net application with only a single thread / main method.

var f1 = new Form();
f1.Show();
var f2 = new Form();
f2.Show();

Depending on how you want these forms to be related though there are some subtle changes you may want to make to the startup code. Can you give us a bit more information on what you're trying to achieve?

JaredPar
I am trying to make a video game and I want to do some introductions before starting the game. Also, by "var"above do you mean the type? Thank you.
George Tyler
@George, `var` is used for type inference in C#. You can just replace it with the type of the `Form`
JaredPar
+2  A: 

Yes you can. You can say new Form() all day long.

jeffamaphone
You would have to tell the `new Form()` to be `Visible = true` to have it pop up on your screen, or `ShowDialog(parentForm)` to make it modal.
Patrick
A: 

They're right, but if you want to edit the form in the designer first use Project -> Add Windows Form and select a name for the form.

This will add another form to the project and let you can open and edit it in the designer.

Homeliss
A: 

You can also use form inheritance if you want a common form design.

Graphain
A: 

var f1 = new Form(); here var is variant datatype in .NET 3.5 which basically object type that can store any objects in it.

Harendra
-1 var is not a variant datatype .. it indicates that the compiler should infer the type from the initialization expression
Ben M