Try something like this:

Type myType = myAssembly.GetType("MyForm");
Form myForm = (Form) Activator.CreateInstance(myType);


You have to cast the object to a form when using CreateInstance.

Brian

-----Original Message-----
From: Dan Souk [mailto:[EMAIL PROTECTED]] 
Sent: Tuesday, April 23, 2002 2:59 AM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] AW: [DOTNET] Load Form by Name


Like I said, just had the impression that there were performance issues. Wasn't saying 
it was or wasn't a problem.

I found the Activator class a little while ago and played around with it, but no luck. 
Seems that the type info is available, but Activator.CreateInstance doesn't create an 
instance of the form.

Thanks anyway.

Dan

-----Original Message-----
From: dotnet discussion [mailto:[EMAIL PROTECTED]] On Behalf Of Urs Eichmann
Sent: Tuesday, April 23, 2002 1:42 AM
To: [EMAIL PROTECTED]
Subject: [DOTNET] AW: [DOTNET] Load Form by Name


I can't quite see your point not to use reflection. Reflection is VERY fast, 
especially compared to the time needed to actually show a form. The framework uses it 
all the time, probably without you even noticing. Also, if you're using Reflection on 
your own assemblies, there shouldn't be a problem with any restrictions. And I guess 
that VB6 also uses some internal reflection algorithm if you do f = Forms("frmMain").

So if you want to give Reflection a try, do something like this (assuming the form to 
display is in the same assembly):

  Dim ty As Type =
System.Reflection.Assembly.GetExecutingAssembly.GetType("frmMain")
  Dim f As System.Windows.Forms.Form = Activator.CreateInstance(ty)
  f.Show()

Best regards,
Urs


-----Ursprüngliche Nachricht-----
Von: Dan Souk [mailto:[EMAIL PROTECTED]] 
Gesendet: Dienstag, 23. April 2002 05:02
An: [EMAIL PROTECTED]
Betreff: Re: [DOTNET] Load Form by Name

Been under the impression that you incur some extra overhead that can slow things 
down. Probably inconsequential in this circumstance I know. Reflection would be 
convenient, but I don't want to get lazy and start reflecting everything. Also, I will 
eventually need to implement some restrictions on the app (similar to the current 
thread about allowing the assembly to be used only from a specific exe) and am 
concerned that I'll put myself behind the proverbial 8-ball when those restrictions 
are implemented.

-----Original Message-----
From: dotnet discussion [mailto:[EMAIL PROTECTED]] On Behalf Of Shawn 
Wildermuth
Sent: Monday, April 22, 2002 7:26 PM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] Load Form by Name


I am not sure if this is available, but I wondered why you want to avoid reflection?  
That seems like the natural case for solving this.

Thanks,

Shawn Wildermuth
[EMAIL PROTECTED]

> -----Original Message-----
> From: dotnet discussion [mailto:[EMAIL PROTECTED]]
> On Behalf Of Dan Souk
> Sent: Monday, April 22, 2002 8:24 PM
> To: [EMAIL PROTECTED]
> Subject: [DOTNET] Load Form by Name
>
>
> I really need to be able to load a form based on the name of a string 
> variable and don't want to have to create a kludge of a select case. 
> The archives only discuss a way to do this via Reflection, which I 
> want to avoid.
>
> Is this possible:
>
> Dim strFormName as string
> Dim frm as Form
> strFormName = "frmMain"
> 'Translate somehow....
> Frm = Forms(strFormName)
> Frm.Show
>
> You could do Forms.Add(strFormName) in VB6, but I don't see a way to 
> do this in .NET.
>
> Thanks for any ideas.
>
> ------------------------
> Dan Souk
> Finna Technologies, Inc.
> 2410 Lindsay Ct
> West Chicago, IL 60185
> +1-630-762-8257 phone
> +1-630-762-8258 fax
> http://www.finnatech.com
>
> You can read messages from the DOTNET archive, unsubscribe from 
> DOTNET, or subscribe to other DevelopMentor lists at 
> http://discuss.develop.com.
>

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe 
to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe 
to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe 
to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or subscribe 
to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to