4

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!

2
  • I edited my post: added the words "ALLOW VARIOUS" , commented out this: // Form fy = new fx(); , added the example of a user event/method call at the bottom of the post. Specifically, I am trying to make the method that will take a form f2, f3, f4, etc as an argument, then display that form. This method could be called from button events on any of the forms, eliminating repetitive code in each. Thanks for your replies! Commented Dec 5, 2014 at 18:53
  • Thanks DBC; after working around a few of my speed bumps in learning, your solution works perfectly! Commented Dec 6, 2014 at 3:45

2 Answers 2

6

You can make a static generic helper method like so:

public static class FormHelper
{
    public static TForm ShowAndActivate<TForm>(TForm form) where TForm : Form, new()
    {
        if (form == null || form.IsDisposed)
        {
            form = new TForm();
            form.Show();
        }
        else
        {
            if (!form.Visible)
            {
                form.Show();
                form.Activate();
            }
            else
            {
                form.Activate();
            }
        }
        return form;
    }
}

The where TForm: Form constraint guarantees that your form variable is a (sub)class of Form. The where TForm : new() constraint guarantees that there is a parameterless constructor for this type of form. More here: http://msdn.microsoft.com/en-us/library/d5x73970.aspx.

By returning a TForm, you inform the caller if a new form was created.

Sign up to request clarification or add additional context in comments.

Comments

0

You can make this method as Extension:

public static class Extensions
{
    public static void displayThatView(this 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 call it:

private void button1_Click(object sender, EventArgs e)   
{
    this.displayThatView();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.