I am working on a WinForms app. There are many instances where I need to display a new screen in the small viewing area, so I am using Panels. Basically, I inherit from panel, expose any properties for the information I need from the panel, anything that needs to happen to display information in the panel happens in it's own code behind. These panels will always be docked full on one parent control (the main form).
I created a generic method to display these panels:
private static T ShowPanel<T>(Control parent, params object[] parameters) where T: Panel
{
T panelToShow = (T)Activator.CreateInstance(typeof(T), parameters);
parent.Controls.Add(panelToShow);
panelToShow.Dock = DockStyle.Fill;
panelToShow.BringToFront();
panelToShow.Show();
return panelToShow;
}
I am using it like this, but I know there has to be a better way to handler the event subscription.
private void ShellButton_Click(object sender, EventArgs e)
{
if (CurrentSelectedSite == null)
{
AlertSelectSite();
return;
}
SystemViewPanel panel = ShowPanel<SystemViewPanel>(this, CurrentSelectedSite.Systems);
panel.SystemsListbox.DoubleClick += new EventHandler(ShellAccessSystemSelected);
}
There are a bunch of buttons that do different things. If a Site has multiple systems, the SystemViewPanel is shown to select which system to perform the action on. With the way I have it now, I have to subscribe to a different named event handler to specify which action I want to perform, so my main form's code is getting cluttered (i.e. ShellAccessSystemSelected, DownloadFileSystemSelected, ViewSystemSystemSelected, etc).
Edit
I think things can be generalized in the fact that I will be showing a panel that allows a user to select a system for most of the tools in my application. However, for each different tool, a different action will be required based on the tool that initiated the SystemViewPanel creation.