[RESOLVED] Instantiate a Form by it's name
I am trying to find a way to instantiate a form by it's name which will be contained in a string variable.
i.e. Instead of doing this:
VB Code:
Dim frm As frmDetails
frm = New frmDetails
I want to do something like this:
VB Code:
Dim frm As Form
Dim strFormName As String
strFormName = "frmDetails"
frm = InstantiateForm(strFormName)
"InstantiateForm" is obviously made up but you get the drift...Something a bit like what the old VB6 CreateObject function used to do.
Re: [2005] Instantiate a Form by it's name
Many people ask for this but very, very few actually need to do it. There is usually a better way. Can you explain why this is required?
Re: [2005] Instantiate a Form by it's name
Basically, I have table which there will be a variable number of alternative data entry forms. These will be stored in a table and the User's chosen form will be the data entry form that is instantiated when they wish to edit a record.
Sure, I could just have a SELECT CASE statement but I thought this would be a more elegant way of doing it...?
Re: [2005] Instantiate a Form by it's name
OK - this can be done thus:-
VB Code:
Imports System.Reflection
Public Class FormUtilities
''' <summary>
''' Loads a new instance of the form passed in
''' </summary>
''' <param name="Formname"></param>
''' <remarks></remarks>
Public Shared Function InstantiateFormByName(ByVal Formname As String) As Form
Dim FormType As System.Type
'\\ Find the type that corresponds to the form name passed in
For Each f As Type In Assembly.GetExecutingAssembly.GetExportedTypes
If f.Name = Formname Then
FormType = f
Exit For
End If
Next
If Not FormType Is Nothing Then
'\\ Get th econstructor that has no parameters
Dim ctor As System.Reflection.ConstructorInfo = FormType.GetConstructor(System.Type.EmptyTypes)
Return CType(ctor.Invoke(Nothing), Form)
End If
End Function
End Class
and an example ofd the usage is:-
VB Code:
Dim fSettings As Form = FormUtilities.InstantiateFormByName("SettingsDialog")
fSettings.ShowDialog(Me)
This was rather hastily knocked together so there's not enough error handling or optimisation but it shows how you can do it...
Re: [2005] Instantiate a Form by it's name
Re: [RESOLVED] Instantiate a Form by it's name
I'm still dubious about whether this is appropriate. I would think that you would know what for you need to use to edit a particular type of record so you should be able to hard-code which form gets created when you're editing each type of record. Is that not so?
Re: [RESOLVED] Instantiate a Form by it's name
It reduces the amount of help that the compiler can give you so shouldn't be used if an alternative is available and I'd also recommend putting more error handling in it if you are putting this in a production system.
However if you make sure that the data in the table is only populated with fully qualified class names for the forms it could be a flexible solution.
Re: [RESOLVED] Instantiate a Form by it's name
Quote:
Originally Posted by jmcilhinney
I'm still dubious about whether this is appropriate. I would think that you would know what for you need to use to edit a particular type of record so you should be able to hard-code which form gets created when you're editing each type of record. Is that not so?
No, that's the point. Different customers may want different data entry forms for the same table.