|
-
Jan 10th, 2007, 05:23 AM
#1
Thread Starter
Fanatic Member
[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.
Last edited by simonm; Jan 10th, 2007 at 07:30 AM.
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
Jan 10th, 2007, 05:43 AM
#2
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?
-
Jan 10th, 2007, 05:48 AM
#3
Thread Starter
Fanatic Member
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...?
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
Jan 10th, 2007, 07:24 AM
#4
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...
-
Jan 10th, 2007, 07:29 AM
#5
Thread Starter
Fanatic Member
Re: [2005] Instantiate a Form by it's name
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
Jan 10th, 2007, 05:41 PM
#6
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?
-
Jan 11th, 2007, 03:10 AM
#7
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.
-
Jan 11th, 2007, 03:37 AM
#8
Thread Starter
Fanatic Member
Re: [RESOLVED] Instantiate a Form by it's name
 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.
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|