Results 1 to 8 of 8

Thread: [RESOLVED] Instantiate a Form by it's name

  1. #1

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    [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:
    1. Dim frm As frmDetails
    2. frm = New frmDetails
    I want to do something like this:
    VB Code:
    1. Dim frm As Form
    2. Dim strFormName As String
    3.  
    4. strFormName = "frmDetails"
    5. 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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    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.

  4. #4
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: [2005] Instantiate a Form by it's name

    OK - this can be done thus:-
    VB Code:
    1. Imports System.Reflection
    2.  
    3. Public Class FormUtilities
    4.  
    5.     ''' <summary>
    6.     ''' Loads a new instance of the form passed in
    7.     ''' </summary>
    8.     ''' <param name="Formname"></param>
    9.     ''' <remarks></remarks>
    10.     Public Shared Function InstantiateFormByName(ByVal Formname As String) As Form
    11.  
    12.         Dim FormType As System.Type
    13.         '\\ Find the type that corresponds to the form name passed in
    14.         For Each f As Type In Assembly.GetExecutingAssembly.GetExportedTypes
    15.             If f.Name = Formname Then
    16.                 FormType = f
    17.                 Exit For
    18.             End If
    19.         Next
    20.         If Not FormType Is Nothing Then
    21.             '\\ Get th econstructor that has no parameters
    22.             Dim ctor As System.Reflection.ConstructorInfo = FormType.GetConstructor(System.Type.EmptyTypes)
    23.             Return CType(ctor.Invoke(Nothing), Form)
    24.         End If
    25.  
    26.     End Function
    27.  
    28. End Class

    and an example ofd the usage is:-
    VB Code:
    1. Dim fSettings As Form = FormUtilities.InstantiateFormByName("SettingsDialog")
    2.         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...

  5. #5

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: [2005] Instantiate a Form by it's name

    OK...thank you kindly.
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    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.

  8. #8

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    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.
    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
  •  



Click Here to Expand Forum to Full Width