Dynamic Object Reference

G

Guest

I have a menu form that has a number of buttons that each launch a different
form. The syntax is the same except for the form name is different. How do
create a common function that launches a new form that excepts the form name
as an argument?

My current syntax:

Private Sub btnNewSearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNewSearch.Click
Dim applicantion As New applicantion
applicantion.MdiParent = Me.MdiParent
applicantion.Show()
End Sub

Private Sub btnDispute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDispute.Click
Dim dispute As New dispute
dispute.MdiParent = Me.MdiParent
dispute.Show()
End Sub

Private Sub btnClientAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClientAdd.Click
Dim clientAdd As New clientAdd
clientAdd.MdiParent = Me.MdiParent
clientAdd.Show()
End Sub

I would like to create a VB function that works like this:

Private Sub launchForm(ByVal formName as string)
Dim form As New formName
form .MdiParent = Me.MdiParent
form .Show()
Sub

But the syntax wouldn't work because formName is not provided until Runtime.

Any thoughts? Thanks.
 
J

Jon Skeet [C# MVP]

jmhmaine said:
I have a menu form that has a number of buttons that each launch a different
form. The syntax is the same except for the form name is different. How do
create a common function that launches a new form that excepts the form name
as an argument?

Use the sender parameter. That should let you find out which button has
been click, and you can have a map from button to which form should be
opened. If you really need to instantiate the forms dynamically though,
you'll need to use something like Activator.CreateInstance.
 
G

Guest

I assume I need to instantiate the form, that way the application can support
multiple instances of the forms with the application. Do you have a sample VB
code that performs this action? Thanks.
 
J

Jon Skeet [C# MVP]

jmhmaine said:
I assume I need to instantiate the form, that way the application can support
multiple instances of the forms with the application. Do you have a sample VB
code that performs this action? Thanks.

As I said, Activator.CreateInstance is the way to go. Just call
Activator.CreateInstance(typeOfFormToConstruct) and it'll return you
one. You'll need to cast it (eg to Form) but that shouldn't be hard.
 
G

Guest

Jon, your solution doesn't appear to work with Option Strict on.

Does anyone else have a VB sample that is close to what I'm trying to
accomplish? Again, I'm trying to create a common function that will except
the name of the form to instatiate and launch. Otherwise I need to repeat the
same code for every button that launches a form. Thanks.

Josh.
 
J

Jon Skeet [C# MVP]

jmhmaine said:
Jon, your solution doesn't appear to work with Option Strict on.

It should do, so long as you cast appropriately.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
Does anyone else have a VB sample that is close to what I'm trying to
accomplish? Again, I'm trying to create a common function that will except
the name of the form to instatiate and launch. Otherwise I need to repeat the
same code for every button that launches a form. Thanks.

You definitely don't need to repeat the code, and you should (IMO) use
a dictionary from sender to the type of form you want to create, and
then Activator.CreateInstance to instantiate the type.
 
G

Guest

I was able to find a code sample online, here is the code:
Sub ShowForm(ByVal T As Type)
Dim F As New Form
F = CType(Activator.CreateInstance(T), Form)
F.MdiParent = Me.MdiParent
F.Show()
End Sub

It is modified from, URL:
http://www.vbcity.com/forums/faq.asp?fid=15&cat=Windows+Forms&

An example of the call:
Private Sub btnNewSearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNewSearch.Click
ShowForm(GetType(applicantion))
End Sub

Josh.
 
G

Guest

If you are not able to provide VB code, can you at least provide pseudo code
to illustrate? If not, is there another MVP or MS member that can? Thanks.

Josh.
 
J

Jon Skeet [C# MVP]

jmhmaine said:
If you are not able to provide VB code, can you at least provide pseudo code
to illustrate? If not, is there another MVP or MS member that can? Thanks.

I can after a while, but it would be easier if you'd post what you've
already got. That's a much more effective form of learning, too.
 
J

Jon Skeet [C# MVP]

jmhmaine said:
I was able to find a code sample online, here is the code:
Sub ShowForm(ByVal T As Type)
Dim F As New Form
F = CType(Activator.CreateInstance(T), Form)
F.MdiParent = Me.MdiParent
F.Show()
End Sub

It is modified from, URL:
http://www.vbcity.com/forums/faq.asp?fid=15&cat=Windows+Forms&

An example of the call:
Private Sub btnNewSearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNewSearch.Click
ShowForm(GetType(applicantion))
End Sub

Right. That all looks okay, except there's no reason to create a new
form at the start of the method and then throw it away again.

As I said, it's just a matter of casting (the CType).
 
G

Guest

Can you alter my code so that it still sets the MdiParent without creating a
new Form?

I've updated my code and put in a BLL class, here it is:

Public Shared Sub ShowFormAsMdiChild(ByVal newFormType As Type, ByVal
mdiParentType As Type)
Dim F As New Form

F = CType(Activator.CreateInstance(newFormType), Form)
F.MdiParent = CType(Activator.CreateInstance(mdiParentType),
Form).ActiveForm
F.Show()
End Sub

A sample call is:
util.ShowFormAsMdiChild(GetType(mainMenu), Me.GetType)

BTW, util is the name of the BLL Class.

Josh.
 
J

Jeffrey Tan[MSFT]

Hi Josh,

Thanks for your post.

I am not sure why you modified your original code like this, however, I
think this should meet your need:
Sub ShowForm(ByVal T As Type)
Dim F As Form 'get rid of the new keyword, so we only has a F
reference of type Form.
F = CType(Activator.CreateInstance(T), Form) 'this will
dynamically create an instance of child form type
F.MdiParent = Me.MdiParent 'set the dynamically created form as
our main form's MDI child.
F.Show()
End Sub

If I misunderstand your request, please feel free to tell me.
==================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jon Skeet [C# MVP]

jmhmaine said:
Can you alter my code so that it still sets the MdiParent without creating a
new Form?

You still need to create a new instance of the appropriate form, but
you don't need to create a new bare System.Windows.Forms.Form as you
currently do with the line:

Dim F As New Form

Just use

Dim F As Form

instead.
 
J

Jeffrey Tan[MSFT]

Hi Josh,

Does our reply make sense to you? Is you still have any concern, please
feel free to feedback, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top