By Bill Cunnien - 4/15/2009
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
|
By Bill Cunnien - 4/15/2009
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
|
By Trent L. Taylor - 4/15/2009
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);
|
By Edhy Rijo - 4/15/2009
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
|
By Bill Cunnien - 4/15/2009
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.
|
By Trent L. Taylor - 4/15/2009
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).
|
By Bill Cunnien - 4/15/2009
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
|
By Trent L. Taylor - 4/15/2009
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.
|
By Bill Cunnien - 4/15/2009
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
|
By Edhy Rijo - 4/15/2009
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
|
By Bill Cunnien - 4/15/2009
...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
|
By Edhy Rijo - 4/15/2009
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
|
|