Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:36:24+00:00 2026-06-13T18:36:24+00:00

Whilst developing a current project which contains a number of WinForms, I’m finding myself

  • 0

Whilst developing a current project which contains a number of WinForms, I’m finding myself becoming cluttered with lines of code simply to handle open / close events for the forms. Currently I’m handling them like so..

//Declare forms
myForm mForm1;
myForm2 mForm2;

private void btnSomething_Click(object sender, EventArgs e)
{
    if (mForm1 == null)
    {
        mForm1 = new myForm();
        mForm1.FormClosed += new FormClosedEventHandler(mForm1_FormClosed);
        mForm1.Show();
    }
    else
        if (mForm1.WindowState == FormWindowState.Minimized)
            mForm1.WindowState = FormWindowState.Normal;
    mForm1.Focus();
}

void mForm1_FormClosed(object sender, FormClosedEventArgs e)
{
    mForm1 = null;
}

And then another set of voids to handle each forms open / close. Now imagine that instead of 2 forms, I’ve got, say, 5 forms. Now I’m even more cluttered. Is there a way to generalize this to have all forms have the same event handlers?

I’ve thought of perhaps using the object sender in an “as” statement, but i’m not sure how i’d find the relevant declared form instance from there.

sender as (form)

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:36:25+00:00Added an answer on June 13, 2026 at 6:36 pm

    You can generalize that code easily:

    //Declare forms
    myForm mForm1;
    myForm mForm2;
    
    private void btnSomething_Click(object sender, EventArgs e)
    {
        ShowOrUpdateForm<myForm>(ref mForm1);
    }
    
    void ShowOrUpdateForm<T>(ref Form form) where T : Form
    {
        if (form == null)
        {
            form = new T();
            form.FormClosed += new FormClosedEventHandler(mForm1_FormClosed);
            form.Show();
        }
        else if (form.WindowState == FormWindowState.Minimized)
            form.WindowState = FormWindowState.Normal;
    
        form.Focus();
    }
    
    void mForm1_FormClosed(object sender, FormClosedEventArgs e)
    {
        // you cannot refactor this easily
        if (sender == mForm1)
            mForm1 = null;
        else if (sender == mForm2)
            mForm2 = null;
    }
    

    If you want to further generalize the closed event handler, you should consider moving forms’ declarations to some sort of an array, list of dictionary. That way you can easily generalize that method.

    EDIT: converted the ShowOrUpdateForm function to generic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Qt project which I have had a debug console displayed whilst
Whilst developing for the iPhone I had a stubborn memory leak that I eventually
Whilst working on some generally horrible Javascript code this morning, I came across the
Whilst developing a php class i have run into a rather interesting thing within
I'm considering developing a medium-size project for a client in IronPython. It's a pretty
I have an application which I am developing using WPF\Prism\MVVM. All is going well
I've been developing my rails apps whilst keeping them as modular as possible. I'm
I am developing a small game prototype in Unity 3.5.5f - in which the
Whilst developing I want to handle some things slight differently than I will when
I am developing an application which is required to keep a certain time t

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.