Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all
I have a project that created all forms at design time, and saved its names in database, now when the application is running, it loads the all forms names from the database and store each one in array as a string.
Now I have the form name as a string, I want to create the form by the name which I have at run time. Can you help me please?

ex:

VB
Dim strFormName As String = "frmMyForm"
Posted

1 solution

try with below method

VB
Public Function TryGetFormByName(frmname As String) As Form
    Dim formType = Assembly.GetExecutingAssembly().GetTypes().Where(Function(a) a.BaseType = GetType(Form) AndAlso a.Name = frmname).FirstOrDefault()

    If formType Is Nothing Then
        Return Nothing
    End If

    Return DirectCast(Activator.CreateInstance(formType), Form)
End Function

refer : Winforms, get form instance by form name[^]
 
Share this answer
 
Comments
fadi77_net 21-May-15 10:48am    
I can see a.Name, Function(a), and a.BaseType
What is a?
fadi77_net 21-May-15 12:32pm    
I tried it but it is not working with me, because my form is not form type Form, I have another type, it is frmBasedChiled, so I want to get an instance of the frmBasedChiled class
DamithSL 21-May-15 13:02pm    
then change Form to frmBasedChiled
fadi77_net 22-May-15 15:00pm    
Thank you very much, it is working with me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900