Open a form using a variable

  • Thread starter Thread starter James White
  • Start date Start date
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
 
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()
///
 
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
 
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.
 
"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
 
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
 
Back
Top