I have 8 forms (and counting) in a program. I have this repetitive code to instantiate OR bring back the "visible" of each form and would like to have a piece of modularized code that would perform these checks shown in this snippet:
//Some form classes:
Form f2 = new Form_DisplayCustomersList();
...and other forms too
...
///The code that I would like to modularize, so I don't have to repeat it for every form:
private void button1_Click(object sender, EventArgs e)
{
//check form status and recreate. Show and activate - as needed.
if ((f2 == null) || f2.IsDisposed)
{
f2 = new Form_DisplayCustomersList();
showForm(f2);
} else
{
if (!f2.Visible)
{
showForm(f2);
f2.Activate();
} else
{
f2.Activate();
}
}
}
So I thought it would be nice to ALLOW VARIOUS button click events call a reusable method that would look something like this:
public void displayThatView(Form fx)
{
if (fx == null || fx.IsDisposed)
{
// Form fy = new fx();
fx.Show;
}
else
{
if (!fx.Visible)
{
fx.Show;
fx.Activate();
}
else
{
fx.Activate();
}
}
}
And to be able to call the method this way, where f2 could be any variable or the Form type:
private void button1_Click(object sender, EventArgs e)
{
this.displayThatView(f2);
}
I am just a few months into C#. I am not sure if it is possible to do what I am trying, but it would eliminate some repetitive code!