We help IT Professionals succeed at work.

Dim a Form

JamesAnthony
JamesAnthony used Ask the Experts™
on
Hi all
Have this code

        Dim NewMDIChild As New MyWorkingForm
        NewMDIChild.MdiParent = AMPSMDI2010
        NewMDIChild.Show()

Where myWorkingform is a form in my project and it works

BUT

I want to do this, should be simple but getting lost


    Dim gForm As New Form
        gForm = "MyworkingForm"

 (Where the "MyWorkingForm" is a passes String rather that a form in the project)


        Dim NewMDIChild As New  gform
        NewMDIChild.MdiParent = AMPSMDI2010
        NewMDIChild.Show()
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
Presuming VB6, you can try this:

        Dim f As Form
        set f = CreateObject("MyworkingForm")
        CallByName(f, "MdiParent", vbSet, AMPSMDI2010)
        f.Show 

Open in new window

in VB 2008 you can just call the form directly from another form.

with gform
.mdiparent=AMPSMDI2010
.show
end with
Top Expert 2015
Commented:
The first code sample won't work because CreateObject is used for COM objects, and the form created by JamesAnthony is definitively a .NET form, not a COM form.

CallByName exists in Visual Basic.NET, but it can be used only to call methods. It cannot be used to instantiate objects.

In order to be able to instantiate an object passed as a String, you need to first know what are the possibles values and do the job in a Select Case:

Dim gForm As Form
Dim yourString As String = "MyworkingForm"

Select Case yourString

  Case "MyworkingForm"
    gForm = New MyworkingForm

  Case "OtherForm"
    gForm = New OtherForm

End Select

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:
You can use reflection to create the instance of form from its name

http://blogs.lessthandot.com/index.php/DesktopDev/MSTech/vb-net-creating-an-instance-of-a-class-u

Author

Commented:
Got me on the right road, wasn't the complete solution,
But
Thanks