Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a way to open a new instance of a form via a sub/function?
I can do it if I specify the form, problem I'm having is passing the form to a sub...

I'm Trying to do something to the effect of...
' This works if I specify the form
Dim NewInstance as New frmTest
NewInstance.Show

' I've tried a dozen ways to get this to work and nadda

   OpenNewInstanceOfForm(frmTest)

Sub OpenNewInstanceOfForm(F as form)
  Dim NewInstance as New F
  NewInstance.Show
End sub

Every time I try to use F, it says it's undefined

Anyone?

Thanks!
Posted

Dim NewInstance as New F will create a problem since F is not a class but rather a variable pointing to an instance.
Try Dim NewInstance as New frmTest
 
Share this answer
 
Comments
bayotle 13-Nov-15 12:15pm    
Thanks for the response Abhinav but the same results as before, that and I can't specify the name of the form in the sub, it would need to be passed since it can be one of a 'many'.

What I'm trying to do essentially is pass a form to a sub in a module so that I can open a new copy of it. This is meant to be generic code used in a template so the form name wont actually be known.
Pass the Type of the form to the method as a parameter, then use Activator.CreateInstance to create an example of the type. Cast that to a Form, and call Show on that.
Alternatively, pass the name of the form class, and use the string version of Activator.CreateInstance to create it.
https://msdn.microsoft.com/en-us/library/d133hta4(v=vs.110).aspx[^]
 
Share this answer
 
Comments
bayotle 13-Nov-15 12:18pm    
Thanks for the quick reply but as of right now, that's slightly over my level of expertise!
I'll have to ponder that for a few, thanks for the link!
OriginalGriff 13-Nov-15 12:41pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900