Open a form using a variable

J

James White

I wish to open a form using a variable derived from a table. The Application is based on the switchboard principle from Access. However in the absence of do.cmd openForm, I have to use this code.
There has to be a better way to instantiate a form surely.

Select Case strArgument.ToUpper
Case "frmDeptMappingsEdit".ToUpper
Dim aa As frmDeptMappingsEdit = New frmDeptMappingsEdit
open_form(aa)
Case "frmDeptMappingstable".ToUpper
Dim aa As frmDeptMappingsTable = New frmDeptMappingsTable
open_form(aa)
....





Private Sub open_form(ByVal frmToOpen As Form)

Try
frmToOpen.Top = 10
frmToOpen.Left = 10
frmToOpen.Owner = Me
frmToOpen.Show()
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
 
H

Herfried K. Wagner [MVP]

There has to be a better way to instantiate a form surely.

Select Case strArgument.ToUpper
Case "frmDeptMappingsEdit".ToUpper
Dim aa As frmDeptMappingsEdit = New frmDeptMappingsEdit
open_form(aa)
Case "frmDeptMappingstable".ToUpper<<<

\\\
Imports System.Reflection
..
..
..
Dim frm As Form = _
DirectCast( _
Activator.CreateInstance( _
Type.GetType("MyApplication.SampleForm") _
), _
Form _
)
frm.Show()
///
 
J

James White

Thanks

I eventually used the Root Namespace as "MyApplication", and it worked. I
cannot find how to get the root namespace programatically, so I hard coded
it for now.
it is worth noting that character case of the form and namespace are
important.

Dim strTemp As String = "Talons2005." & strFormName

Dim frmToOpen As Form = Activator.CreateInstance(Type.GetType(strTemp))

frmToOpen = DirectCast(frmToOpen, Form)

frmToOpen.Top = 10

frmToOpen.Left = 10

frmToOpen.Owner = Me

frmToOpen.Show()

many Thanks
--
James White
CSH Consultants
MEL/AU
 
P

Peter Huang [MSFT]

Hi

We need the type instance to get the namespace.
e.g.
MsgBox(Me.GetType().Namespace)

If you have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Armin Zingler

"Peter Huang" said:
Hi

We need the type instance to get the namespace.
e.g.
MsgBox(Me.GetType().Namespace)

If you have any concern, please feel free to post here.


VB 2005 code? Namespace is not a property in Framework 1.1.

As one doesn't have to specify a root namespace, it might be empty. The root
namespace is only a simplification used by the compiler, but concerning the
assemblies and reflection (and maybe the CLS in general; I don't know), I
don't think there is a "root namespace" at all.


Armin
 
H

Herfried K. Wagner [MVP]

Armin Zingler said:
VB 2005 code? Namespace is not a property in Framework 1.1.

Sure, it is a property of the 'Type' class even in .NET 1.1.
 
A

Armin Zingler

Herfried K. Wagner said:
Sure, it is a property of the 'Type' class even in .NET 1.1.

Arghh... was pressing "N" in the object browser - but it's there in the
escaped version: "[Namespace]". Don't know why this was introduced at all in
the object browser. Not necessary, I think. Only necessary in the source
code.

..... Argh again ;-) This is also the reason why "GetType" wasn't found while
writing this reply...

Armin
 

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