Introduction
This article will introduce a way to reduce the code that is used to open various forms from an applications main menu.
The Problem
As with most Windows applications, the main form will mainly be used to open a lot of other different forms, depending on the application's purpose.
The usual way would be to handle the button's "Click" event, and to instantiate an instance of the form, and to "ShowDialog".
When the application needs to open a lot of forms, these "Click" events for each button could cause the code to become very messy quickly.
I therefore set out to try and find an easier way to reduce the code needed to open all the different forms.
In the sections below, I will explain the solution I came up with.
The Solution
Every component has a "Tag
" property that takes an Object
as value. This means that we can save any value we want in it.
For the proposed solution, we will make use of this Tag
property to save the name of the form we want to open. For example, if we want a button to launch a form named "frmTest
", we use the designer properties to set the Tag
property to "frmTest
". Take note: the name of the form is NOT the value in the "Text
" property, it is the value in the "Name
" property.
We have only ONE method that will take care of launching all the various forms.
The LaunchForm
method is the method we link to the Click event of the button. This can easily be done with the designer properties. Because we need to link this method to the button's Click event, we need to specify the two parameters required. The parameters are:
sender
of type
object, which is the control that the Click
event belongs to e
of type EventArgs
, which is the event arguments associated with the Click
event
internal void LaunchForm(object sender, EventArgs e)
{
String FormName = String.Empty;
try
{
ToolStripMenuItem item = sender as ToolStripMenuItem;
FormName = Assembly.GetExecutingAssembly().FullName.Split(',')[0] +
"." + item.Tag.ToString(); ;
Type uiType = Assembly.GetExecutingAssembly().GetType(FormName);
using (Form form = Activator.CreateInstance(uiType) as Form)
{
form.StartPosition = FormStartPosition.CenterScreen;
form.Text = "Demo - " + item.Text;
form.Icon = GetIconFromImage(item.Image);
form.ShowDialog();
}
}
catch (Exception ex)
{
MessageBox.Show(String.Format
("Error opening window: {0}. Error Message: {1}", FormName, ex.Message));
}
}
A form's full name consists of the name of the form, as well as the namespace in which it resides.
If we look at the code, we can see that with a little bit of reflection we can easily obtain the namespace of the form, and by combining it with the name specified in the Tag property.
Once we have an instance of the Form we want, we are free to do all sorts of manipulations to the forms we want to launch. In the sample code above, we set the StartPosition
. We also use the sender parameter to determine the type of control used to launch the form. By doing this, we can access the Text, as well as other properties such as the image, etc….
By launching the forms in this way, the code is in one centralised location, which makes maintaining it very easy!
Below is a screenshot of which properties must be set. The screenshot is of a ToolStripMenuItem
that opens a form called "frmTest
". As you can see, the Tag
property is set to "frmTest
", and the Click
event is set to call "LaunchForm
". That's all you need!
Please let me know what you think, and if you found this code useful! I welcome any feedback!
History
- 31st October, 2008 - Initial version
I am currently employed by Inivit Systems. Here we strive to use cutting edge technologies, in order to provide our clients with the best possible software and continue to offer them a competitive edge.
We are responsible for all kinds of applications, ranging from web applications to desktop applications, including integration into some older legacy systems.
I love programming and solving puzzles, but beside that I enjoy experimenting with new technologies, reading, watching movies and relaxing with friends.