Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the list of forms in VS2008 C# project?

Tags:

c#

winforms

I want to get a list of all the forms in the project i am running a form from .

Suppose i am running a project which has 4 forms 1.Form1 2.Form2 3.Form3 4.Form4

and i want to retrieve the list of them for further direction which form to direct to

like image 576
Mobin Avatar asked Mar 19 '10 11:03

Mobin


People also ask

How do you display a form in C #?

Writing C# Code to Display a Modal FormPress F5 once again to build and run the application. After pressing the button in the main form to display the sub form you will find that the main form is inactive as long as the sub form is displayed. Until the sub form is dismissed this will remain the case.

How do I toggle between forms in C#?

The trick is to use Application. Run() without parameters and Application. Exit() at the point where you want to exit the application. Now when you run the application, Form1 opens up.


1 Answers

Do you mean:

  1. Design-time?
  2. Run-time?

If run-time, do you mean:

  1. All forms defined in the project?
  2. All open forms

If at Design-time, then I don't know.

If you mean at run-time, and you want all forms declared, you need to resort to reflection. Iterate through all types in your assembly(/ies) and find all types inheriting from the Form class.

Something like this would do:

Type formType = typeof(Form);
foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
   if (formType.IsAssignableFrom(type))
   {
       // type is a Form
   }

If you mean at run-time, and you want all open forms, you can use Application.OpenForms.

like image 78
Lasse V. Karlsen Avatar answered Nov 05 '22 02:11

Lasse V. Karlsen