Geeks With Blogs
Flippin IT The blog of Andrew Winn

Strange as it maybe there is no built-in collection to give you a list of Forms in you Windows Application.

This function returns a SortedList Collection of Forms in the Windows Application,

VB Code:


    Public Shared Function GetForms() As Generic.SortedList(Of String, Type)
            ' Define the base type
            Dim baseType As Type = GetType(Form)
            Dim forms As New Generic.SortedList(Of String, Type)
            ' Get all types in the current assembly
            Dim types() As Type = System.Reflection.Assembly.GetExecutingAssembly.GetTypes
            For i As Integer = 0 To types.Length - 1
                Dim T As Type = types(i)
                If T.IsSubclassOf(baseType) Then
                    Dim formName As String = T.UnderlyingSystemType.Name
                    If forms.ContainsKey(formName) = True Then
                        MessageBox.Show(String.Format("Duplicate form {0} found, please check", formName))
                    End If
                    forms.Add(formName, T)
                End If
            Return forms
        Catch ex As Exception
        End Try
    End Function

C# Code:-


public static SortedList<string, Type> GetForms()
            // Define the base type
            Type baseType = typeof(Form);
            SortedList<string, Type> forms = new SortedList<string, Type>();
            // Get all types in the current assembly
            Type[] types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();
            for (int i = 0; i <= types.Length - 1; i++)
                Type T = types[i];
                if (T.IsSubclassOf(baseType))
                    string formName = T.UnderlyingSystemType.Name;
                    if (forms.ContainsKey(formName))
                        MessageBox.Show(string.Format("Duplicate form {0} found, please check", formName));
                    forms.Add(formName, T);
            return  forms;
        catch (Exception ex)


Posted on Friday, February 27, 2009 4:54 PM WinForms | Back to top

Comments on this post: Getting a list of forms from your Windows Application

# re: Getting a list of forms from your Windows Application
Requesting Gravatar...
Hello Andrew,

I'm curious as to why you would ever want a list of the class names (type names) of all the types in your application that derive from System.Form?

This is not a "list of forms in your windows application", which I would iterpret as a list of *instances* of types derived from System.Form, not simply a type sitting in the assembly.

Also, I think you have to check and make sure the type is not abstract, because then you really getting a misleading list in that it includes something that cannot be instatiated.
Left by Joe on Mar 01, 2009 2:51 AM

# re: Getting a list of forms from your Windows Application
Requesting Gravatar...
private SortedList<String, Type> GetForms()
SortedList<String, Type> result = new SortedList<string, Type>();
Type baseType = typeof(Form);
foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
if (!t.IsAbstract &&
t.IsClass &&
result.Add(t.FullName, t);
return result;

1. If "translated" to visual basic, remember to use "AndAlso" so that the optimization of checking that the type is actually a Class happens before the costly IsSubclassOf so the expression can exit without evaluating all conditionals.
2. Why catch an exception you are not handling?
3. Don't return abstract types.
4. Don't need the UnderlyingType.
5. It is easily possible to have the same type name for two totally different types, as long as a different namespace is used. Therefore, use t.FullName for the Key, and forget the key check.
6. Why not just return a list of Types rather than a dictionary?

Happy programming.
Left by Joe on Mar 01, 2009 3:11 AM

Your comment:
 (will show your gravatar)

Copyright © Flippin IT | Powered by: