Creating Windows Forms at runtime using System.Reflection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bisbal

    Creating Windows Forms at runtime using System.Reflection

    Hi,

    I'd like to know if it is possible to create a form type from a string. My
    case: I have a DataTable with forms names (i.e frm_001_UserInf o.cs), and I'd
    like to create an instance of that form from my code dynamically, retrieving
    the form name from the database.

    Something like str = "frm_001_UserIn fo.cs" and then create an instance of
    the form using str.

    For what I've read about System.Reflecti on, there might be a way to do this,
    but I dont know how.

    Any help would be greatly appreciated.

    Thanks in advance.
  • Mohamoss

    #2
    RE: Creating Windows Forms at runtime using System.Reflecti on

    hi
    i think this article on the MSDN can help a lot


    ml/vbnet10082002.a sp
    Hope it does
    Mohamed Mahfouz
    MEA Developer Support Center
    ITworx on behalf of Microsoft EMEA GTSC

    Comment

    • avnrao

      #3
      Re: Creating Windows Forms at runtime using System.Reflecti on

      u can use
      System.Reflecti on.Assembly.Get ExecutingAssemb ly().CreateInst ance(formName)

      and pass in your form name. but make sure that your form type exists in the
      current assembly. If not, you have load the assembly that contains the form
      and create an instance of that form.

      hth

      "Bisbal" <Bisbal@discuss ions.microsoft. com> wrote in message
      news:8752B2D4-394A-49D2-AD23-487170C1BDC9@mi crosoft.com...[color=blue]
      > Hi,
      >
      > I'd like to know if it is possible to create a form type from a string. My
      > case: I have a DataTable with forms names (i.e frm_001_UserInf o.cs), and[/color]
      I'd[color=blue]
      > like to create an instance of that form from my code dynamically,[/color]
      retrieving[color=blue]
      > the form name from the database.
      >
      > Something like str = "frm_001_UserIn fo.cs" and then create an instance of
      > the form using str.
      >
      > For what I've read about System.Reflecti on, there might be a way to do[/color]
      this,[color=blue]
      > but I dont know how.
      >
      > Any help would be greatly appreciated.
      >
      > Thanks in advance.[/color]


      Comment

      • DavidT

        #4
        RE: Creating Windows Forms at runtime using System.Reflecti on

        Mohamoss,

        I use:

        Dim GenericInstance As Object
        GenericInstance = Activator.Creat eInstance(TypeT oLoad)

        Dim FormToShow As Form = CType(GenericIn stance, Form)

        from the example to load a form that does not has parameters in its
        constructor, everything work fine.

        However, some of our forms need input parameters for the constructor, so I
        use the following code:

        Dim frm() As Form = {Me} ' Me is the calling form
        Dim formActivator As Object = Activator.Creat eInstance(formO bject, frm)

        I keep getting the following error message at the above line:

        "An unhandled exception of type 'System.Argumen tNullException' occurred in
        mscorlib.dll. Additional information: Value cannot be null."

        Do you have any ideas what happen

        Thanks

        DavidT

        "Mohamoss" wrote:
        [color=blue]
        > hi
        > i think this article on the MSDN can help a lot
        >
        > http://msdn.microsoft.com/library/de...us/dnadvnet/ht
        > ml/vbnet10082002.a sp
        > Hope it does
        > Mohamed Mahfouz
        > MEA Developer Support Center
        > ITworx on behalf of Microsoft EMEA GTSC
        >
        >[/color]

        Comment

        Working...