How to get the list of forms in VS2008 C# project?
I want to get a list of all the for开发者_如何转开发ms 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
Do you mean:
- Design-time?
- Run-time?
If run-time, do you mean:
- All forms defined in the project?
- 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.
精彩评论