tags:

views:

45

answers:

1

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

+3  A: 

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.

Lasse V. Karlsen
yes i think i want that .but where do i get the object for the form which satisfies the "if" condition if you may please
Mobin
Do you mean that last case? You can't, that was the point, either you want the live, existing, form objects, or you want all possible types, which may or may not exist yet. You would need to combine the two if you want both. Can you please elaborate what you want? And "yes I think I want that", when I've listed multiple things doesn't help me help you, so say *what* you want.
Lasse V. Karlsen
OK then i would say i want list of objects of forms on RUNTIME and ALL FORMS DEFINED IN THE PROJECT hope this helps you understand my query and yes i get the point that it was not a proper way to tell my intentions lolz
Mobin
So what specifically is it that you're after then since my answer seems to not answer your question. That is, I figure it doesn't since you keep commenting. All runtime forms are Application.OpenForms, link at the bottom of my answer. All forms defined in the project is the code I posted. Does neither do what you want?
Lasse V. Karlsen