Creating an Instance of a Form From a String


Author
Message
Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K 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
Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)
Group: Forum Members
Posts: 785, Visits: 3.6K
By the way, if I change the code to this:



(Activator.CreateInstance(Type.GetType("Aspire.Sales.CustomerInvoices")) as Form).Show();




I get the following error:



Value cannot be null.\rParameter name: type




Obviously, the Type.GetType() function is not returning any type. I have confirmed the string. And, I have used several variations. No dice.



I started working on this yesterday afternoon. I am stumped. I had hoped a good night's sleep would provide a better perspective, but, alas!, it has not.



Bill

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
If you are going to create it off of a string, you have to pass the full name, not just the name of the class.  This can be a long discussion in and of itself, but in super Reader's Digest break down, and to save you some time, just create an instance of your object and then pull the full name off of it so you can see what it is:

MyObject obj = new MyObject();

MessageBox.Show(obj.GetType().FullName);


Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K 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

Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)
Group: Forum Members
Posts: 785, Visits: 3.6K
Yup...I did that and the response was the same string that I used in the earlier post: "Aspire.Sales.CustomerInvoices". I remember running this kind of script yesterday and getting something totally different, like "Aspire.Sale.CustomerInvoices, CustomerInvoices, 1.0.0.0, blah blah". I'll see if I can get back to that kind of string.
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
You may need to also include the assembly name and version as well if you are not pulling a class from the same assembly (and sometimes even if you are).
Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)
Group: Forum Members
Posts: 785, Visits: 3.6K
The code I was using yesterday was the following:



String mFullName = mForm.GetType().AssemblyQualifiedName;




That produces the following string:



Aspire.Sales.CustomerInvoices, Sales, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null




If I use that, the form fires up perfectly. I'll clean up my code and post what I did for future reference.



Edhy, I do need to review your code, since I'd rather not have to hard code the AssemblyQualifiedName in every LinkItem. As soon as the assembly version ramps up, my NavBar is broken.



Thanks,

Bill

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Yup...one thing you can try also is to leave out the version in that string so if you are "hard coding" this and you change a version, it won't die.
Bill Cunnien
Bill Cunnien
StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)StrataFrame VIP (1.1K reputation)
Group: Forum Members
Posts: 785, Visits: 3.6K
I place the AssemblyQualifiedName into the tag of the NavBarItem, then point the LinkClicked event to this code:





private void OpenForm(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)

{

String mFullName = (String)((DevExpress.XtraNavBar.NavBarItem)sender).Tag;

Boolean IsOpen = false;

foreach (Form mOpenForm in Application.OpenForms)

{

String mOpenFullName = mOpenForm.GetType().AssemblyQualifiedName;

if (mOpenFullName == mFullName)

{

IsOpen = true;

mOpenForm.Activate();

}

}

if (!IsOpen) { (Activator.CreateInstance(Type.GetType(mFullName)) as Form).Show(); }

}





Since I am in full control of the assemblies and their versions, I think that this may be the best and easiest route to take. I'll comment out this code and call it complete.



Thanks!!

Bill
Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Bill Cunnien (04/15/2009)
Edhy, I do need to review your code, since I'd rather not have to hard code the AssemblyQualifiedName in every LinkItem. As soon as the assembly version ramps up, my NavBar is broken.



Humm, I am using Infragistics's UltraToolbarManager and now that you mention it, this could also happen to me at any rate. Currently I am manually setting the Key property of each tool's item with a string like this: "CardTrackingSystem.frmInventory" and so far it works, I could also just enter the form's name "frmInventory" and have the LaunchDialog get the assembly info to avoid hard coding the full name all the time, but of course any way to automate the process and make it more reliable is always welcome, so I'll be watching this post for more info Smile

Edhy Rijo

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search