Dynamically selecting a form [modified]
-
Hi there, In my VS2008 project, I start with a form used as a switchboard: 8 buttons and labels are placed on the form, and the textproperty of the labels is filled according to values found in a database. The table in that database holds the text to be displayed, as well as the name of the form to be opened when the corresponding button is clicked. How should I write my code so that the appropiate form is opened, without using a 'Select Case' or 'If Then Else', but by using the forms name as argument ? I'm using VB.Net, but an example in C# is also appreciated. Thanks in advance.
modified on Thursday, May 8, 2008 7:00 AM
-
Hi there, In my VS2008 project, I start with a form used as a switchboard: 8 buttons and labels are placed on the form, and the textproperty of the labels is filled according to values found in a database. The table in that database holds the text to be displayed, as well as the name of the form to be opened when the corresponding button is clicked. How should I write my code so that the appropiate form is opened, without using a 'Select Case' or 'If Then Else', but by using the forms name as argument ? I'm using VB.Net, but an example in C# is also appreciated. Thanks in advance.
modified on Thursday, May 8, 2008 7:00 AM
I've frequently written code where an action relates to a class to be executed. Your process is similar. So let's assume that all of your forms are compiled into a separate assembly: Mycompany.Myapplication.DynamicFormsAssembly.dll and the dll name matches the assembly name.
void ShowDynamicForm( System.Windows.Forms.Label sender ) { Assembly assembly = Assembly.GetAssembly( typeof( Mycompany.Myapplication.DynamicFormsAssembly ) ); Form dynamicForm = ( Form )assembly.CreateInstance( sender.TextProperty ); dynamicForm.Parent = this; dynamicForm.Show(); }
Good luck doing this in VB.