Creating an Instance of a Form From a String


Author
Message
Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)
Group: Forum Members
Posts: 785, Visits: 3.6K
This is turning out to be a little more difficult than I expected. I used the forum entry to guide me but I have run into a problem with the following code:



((Form)Activator.CreateInstance(mFormName, "Form")).Show();




The error that I get when I build the project is:



Cannot convert type 'System.Runtime.Remoting.ObjectHandle' to 'System.Windows.Forms.Form'




I have tried simply passing the string of the form ("MyNamespace.MyForm") to CreateInstance, but that does not work, either.



Any direction that you can provide would be helpful.

Bill
Reply
Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Bill,

I hard code the full form name on a property of the menu items and then call my version of a LaunchDialog method which is a collection of many versions here in the forums, I'll post it here so you can check it out:





Private Sub LaunchDialog(ByVal dialogType As System.Type)

'-- All new forms are based on the following class:

' CardTrackingSystem.UI.Windows.Forms.ApplicationBaseForm

Try

Dim newFormToBeLaunched As CardTrackingSystem.UI.Windows.Forms.ApplicationBaseForm

newFormToBeLaunched = CType(Activator.CreateInstance(dialogType), Form)



'-- Check if the form exist in the MDI collection and if so, bring it to the from.

For Each currentLoadedForm As Form In Me._MDIClient.Controls

If currentLoadedForm.Name.Equals(newFormToBeLaunched.Name, StringComparison.CurrentCultureIgnoreCase) Then

'-- IF the form is minimized then restore it.

If currentLoadedForm.WindowState = FormWindowState.Minimized Then

currentLoadedForm.WindowState = FormWindowState.Normal

End If



'-- Dispose the newly created instance and use the current one

newFormToBeLaunched.Dispose()

newFormToBeLaunched = Nothing



'-- Show the form on top of the others.

currentLoadedForm.BringToFront()



Exit Sub

End If

Next



'-- If the form was not found as part of the MDI control, then add it

AddFormHandlers(newFormToBeLaunched)



'-- Set the parent MDI

newFormToBeLaunched.MdiParent = Me



'-- Show the dialog

newFormToBeLaunched.Show()



'-- Update the Open Window Items

SetOpenWindowsItems(CType(newFormToBeLaunched, ApplicationBaseForm))



Catch ex As Exception

Me.ShowMessageByKey("FormNoAvailableYet", My.Application.Info.Title)

End Try



End Sub



Edhy Rijo

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Threaded View
Threaded View
Bill Cunnien - 16 Years Ago
Bill Cunnien - 16 Years Ago
Edhy Rijo - 16 Years Ago
Trent L. Taylor - 16 Years Ago
Bill Cunnien - 16 Years Ago
Trent L. Taylor - 16 Years Ago
                     The code I was using yesterday was the following:
...
Bill Cunnien - 16 Years Ago
                         Yup...one thing you can try also is to leave out the version in that...
Trent L. Taylor - 16 Years Ago
                             [quote]...one thing you can try also is to leave out the version in...
Bill Cunnien - 16 Years Ago
                                 Hi Bill,
Glad to be able to help. I also borrowed some idea from...
Edhy Rijo - 16 Years Ago
                         I place the AssemblyQualifiedName into the tag of the NavBarItem, then...
Bill Cunnien - 16 Years Ago
                         [quote][b]Bill Cunnien (04/15/2009)[/b][hr]Edhy, I do need to review...
Edhy Rijo - 16 Years Ago

Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search