Creating an Instance of a Form From a String


Author
Message
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,

Glad to be able to help. I also borrowed some idea from your code to use the AssemblyQualifiedName and just type the form name like "frmInventory" in the Tool.Key property, in my case I always prefix all my forms with "frm", this will make my code more standard so I can use it other projects without having to do many Find/Replace changes.

Here is the code I used in the UltraToolbarManager1_ToolClick() event:





Dim ItemKeyStr As String = e.Tool.Key

If Not String.IsNullOrEmpty(ItemKeyStr) Then

If ItemKeyStr.StartsWith("frm", StringComparison.CurrentCultureIgnoreCase) Then

'-- If this is a form its name should start with "frm", so lets get the main form's AssemblyQualifiedName

' and remove the [, Version=] string and replace its name with the ItemKeyStr value which should be the

' form's name to be open.

Dim mOpenFullName As String = String.Empty

Dim AssemblyQualifiedName As String = Me.GetType().AssemblyQualifiedName

mOpenFullName = AssemblyQualifiedName.Remove(AssemblyQualifiedName.IndexOf(", Version=")).Replace(Me.Name, ItemKeyStr)

Me.LaunchDialog(Type.GetType(mOpenFullName))



' Me.LaunchDialog(Type.GetType(ItemKeyStr))

Else

'-- Process all custom forms and options here.

Select Case e.Tool.Key

Case "DatabaseConnection" ' ButtonTool

'-- Set the connections

Me.SetDatabaseConnection()



'-- TODO: close all child forms to force them to use the new connection string



Case "SecurityMaintenanceForm" ' ButtonTool

Me.ShowSecurityDialog()



Case "Exit" ' ButtonTool

Me.Close()



End Select

End If



End If



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
...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.




Excellent! I whacked everything from ", Version=" and following...the code works perfectly. That will resolve that "hard coding" problem.



I enter the string "Aspire.Sales.CustomerInvoices, Sales" into the tag property of the NavBarItem, then run this code for the LinkClicked event:





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;

mOpenFullName = mOpenFullName.Remove(mOpenFullName.IndexOf(", Version="));

if (mOpenFullName == mFullName)

{

IsOpen = true;

if (mOpenForm.WindowState == FormWindowState.Minimized) { mOpenForm.WindowState = FormWindowState.Normal; }

mOpenForm.Activate();

}

}

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

}





I did add a tweak for the windowstate (thanks, Edhy!!). All is well.



Sighing contentedly,

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

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
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
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
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
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.
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

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);


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