|
Bill Cunnien
|
|
|
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
|
|
|
|
|
Trent Taylor
|
|
|
Group: StrataFrame Developers
Posts: 6.6K,
Visits: 7K
|
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
|
|
|
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
|
|
|
Group: StrataFrame Developers
Posts: 6.6K,
Visits: 7K
|
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
|
|
|
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
|
|
|
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
|
|
|
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
|
|
|
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
Edhy Rijo
|
|
|
|