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 1099187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:41:45+00:00 2026-05-17T00:41:45+00:00

I’m studying MDI Form in windows form and I’m playing with this simple application:

  • 0

I’m studying MDI Form in windows form and I’m playing with this simple application:
alt text

Each ToolStripMeniItem calls a single instance of a particular form, but as you can see (please see my code) my code is repetitive for each ToolStripMeniItem, how can I shorten this?

        public static Form IsFormAlreadyOpen(Type FormType)
        {
            foreach (Form OpenForm in Application.OpenForms)
            {
                if (OpenForm.GetType() == FormType)
                    return OpenForm;
            }
            return null;
        }

        private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1 f1 = null;
            if (IsFormAlreadyOpen(typeof(Form1)) == null)
            {
                f1 = new Form1();
                f1.MdiParent = this;
                f1.Show();
            }
            else
            {
                Form selectedForm = IsFormAlreadyOpen(typeof(Form1));
                foreach (Form OpenForm in this.MdiChildren)
                {
                    if (OpenForm == selectedForm)
                    {
                        if (selectedForm.WindowState == FormWindowState.Minimized)
                        {
                            selectedForm.WindowState = FormWindowState.Normal;
                        }
                        selectedForm.Select();
                    }
                }
            }
        }

        private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = null;
            if (IsFormAlreadyOpen(typeof(Form2)) == null)
            {
                f2 = new Form2();
                f2.MdiParent = this;
                f2.Show();
            }
            else
            {
                Form selectedForm = IsFormAlreadyOpen(typeof(Form2));
                foreach (Form OpenForm in this.MdiChildren)
                {
                    if (OpenForm == selectedForm)
                    {
                        if (selectedForm.WindowState == FormWindowState.Minimized)
                        {
                            selectedForm.WindowState = FormWindowState.Normal;
                        }
                        selectedForm.Select();
                    }
                }
            }
            // and so on... for the other ToolStripMeniItem
        }
  • 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-05-17T00:41:46+00:00Added an answer on May 17, 2026 at 12:41 am

    A generic function is a function that will work with a set of types that will be defined by the caller

    so instead of
    int doubleIt(int a) { return a*2; }
    you can say
    T doubleIt<T>(T a){ return a*2; }

    this means you can define one function that can be called like this:

    int intResult = doubleIt(...pass in a int..);
    double doubleResult = doubleIt(...pass in a double..);
    float floatResult = doubleIt(...pass in a float..);
    

    So for your code you create a method that takes the form type you’re creating as a value for T.

    However the compiler doesn’t know that T is a form and that you can call new on it, so you must contrain the types that can be passed as T with the where clause saying T must be a form and must a constructor that you can access

    You therefore replace the core functionality with a generic function that is specialised to the type of f1 that you need.

    so instead of Form f1 = null; you have a generic type that you pass in T f1 = null;

    private void ClickEvent<T>(object sender, EventArgs e) where T: Form, new() {
                T f1 = null;
        .
        .
        .
     }
    

    Then call this method from the real event handlers

    private void form1ToolStripMenuItem_Click(object sender, EventArgs e)       {
                ClickEvent<Form1>(sender, e)
            }
    
            private void form2ToolStripMenuItem_Click(object sender, EventArgs e) {
               ClickEvent<Form1>(sender, e) 
            }
    

    So you end up with something like:

    public static Form IsFormAlreadyOpen(Type FormType)
    {
        foreach (Form OpenForm in Application.OpenForms)
        {
            if (OpenForm.GetType() == FormType)
                return OpenForm;
        }
        return null;
    }
    
    private void ClickEvent<T>(object sender, EventArgs e) where T: Form, new() 
        {
        T f1 = null;
        if (IsFormAlreadyOpen(typeof(T)) == null)
        {
            f1 = new T();
            f1.MdiParent = this;
            f1.Show();
        }
        else
        {
            Form selectedForm = IsFormAlreadyOpen(typeof(T));
            foreach (Form OpenForm in this.MdiChildren)
            {
                if (OpenForm == selectedForm)
                {
                    if (selectedForm.WindowState == FormWindowState.Minimized)
                    {
                        selectedForm.WindowState = FormWindowState.Normal;
                    }
                    selectedForm.Select();
                }
            }
        }
    }
    
        private void form1ToolStripMenuItem_Click(object sender, EventArgs e)       {
            ClickEvent<Form1>(sender, e)
        }
    
    private void form2ToolStripMenuItem_Click(object sender, EventArgs e) {
       ClickEvent<Form1>(sender, e) 
    }
    

    Extra: If you can use Linq, then something like this.

      using System.Linq;
    
                 public static Form IsFormAlreadyOpen(Type FormType)        {       
                    return Application.OpenForms.Where( f => f.GetType() == FormType).FirstOrDefault();
                }
    
                private void ClickEvent<T>(object sender, EventArgs e) where T: Form, new() 
                {
                    T selectedForm = IsFormAlreadyOpen(typeof(T); 
                    if (selectedForm == null)             {
                        (new T() { MdiParent = this; }).Show()
                    }
                    else {  
                        this.MdiChildren.Where(o => o == selectedForm).ForEach(
                            openForm => { 
                                selectedForm.WindowState = (selectedForm.WindowState == FormWindowState.Minimized) ? FormWindowState.Normal : selectedForm.WindowState;
                                openForm.Select();
                            }
                        );
                    }
                }
                private void form1ToolStripMenuItem_Click(object sender, EventArgs e)       {
                    ClickEvent<Form1>(sender, e)
                }
    
                private void form2ToolStripMenuItem_Click(object sender, EventArgs e) {
                   ClickEvent<Form2>(sender, e) 
                }
    

    There are other things you can do to shorten this code, however the use of generics and linq will be the most effective.

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

Sidebar

Related Questions

No related questions found

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.