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.

Re: Dynamic Object Reference by Jon

Jon
Sun Jun 19 03:06:25 CDT 2005

jmhmaine <jmh@online.nospam> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Dynamic Object Reference by jmh

jmh
Sun Jun 19 07:31:02 CDT 2005

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.

"Jon Skeet [C# MVP]" wrote:

> jmhmaine <jmh@online.nospam> wrote:
> > 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.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

Re: Dynamic Object Reference by Jon

Jon
Sun Jun 19 11:35:41 CDT 2005

jmhmaine <jmh@online.nospam> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Dynamic Object Reference by jmh

jmh
Sun Jun 19 12:34:02 CDT 2005

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.

"Jon Skeet [C# MVP]" wrote:

> jmhmaine <jmh@online.nospam> wrote:
> > 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.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

Re: Dynamic Object Reference by Jon

Jon
Sun Jun 19 13:14:06 CDT 2005

jmhmaine <jmh@online.nospam> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Dynamic Object Reference by jmh

jmh
Sun Jun 19 15:40:01 CDT 2005

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.

"jmhmaine" wrote:

> 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.
>
> "Jon Skeet [C# MVP]" wrote:
>
> > jmhmaine <jmh@online.nospam> wrote:
> > > 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.
> >
> > --
> > Jon Skeet - <skeet@pobox.com>
> > http://www.pobox.com/~skeet
> > If replying to the group, please do not mail me too
> >

Re: Dynamic Object Reference by jmh

jmh
Sun Jun 19 15:40:45 CDT 2005

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.

"Jon Skeet [C# MVP]" wrote:

> jmhmaine <jmh@online.nospam> wrote:
> > 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.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

Re: Dynamic Object Reference by Jon

Jon
Sun Jun 19 15:53:55 CDT 2005

jmhmaine <jmh@online.nospam> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Dynamic Object Reference by Jon

Jon
Sun Jun 19 15:55:06 CDT 2005

jmhmaine <jmh@online.nospam> wrote:
> 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).

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Dynamic Object Reference by jmh

jmh
Sun Jun 19 17:19:01 CDT 2005

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.

"Jon Skeet [C# MVP]" wrote:

> jmhmaine <jmh@online.nospam> wrote:
> > 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).
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

Re: Dynamic Object Reference by v-jetan

v-jetan
Sun Jun 19 21:10:44 CDT 2005

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.


Re: Dynamic Object Reference by Jon

Jon
Mon Jun 20 01:03:53 CDT 2005

jmhmaine <jmh@online.nospam> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Dynamic Object Reference by v-jetan

v-jetan
Wed Jun 22 02:05:19 CDT 2005

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.