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

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