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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:53:15+00:00 2026-06-01T03:53:15+00:00

How do I create a function for following code so that i may not

  • 0

How do I create a function for following code so that i may not have to write the following whole code to make a form be used as MDICHILD Form.

Students stu = null;
    private void studentsToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        if (stu == null || stu.IsDisposed)
        {
            stu = new Students();
            stu.MdiParent = this;
            stu.Show();
        }
        else
        {
            stu.Activate();
        }
    }

while I want it like this

private void studentsToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        CreateMdiChild(Students);
    }

and function should be like this

public void CreateMdiChild(Form form)
    {
        //expected code            
    }
  • 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-01T03:53:17+00:00Added an answer on June 1, 2026 at 3:53 am

    You could make the method generic, e.g. :

    public void CreateMdiChildOrActivate<T>(ref T instance) where T : Form, new()
    {
        if (instance == null || instance.IsDisposed)
        {
            instance = new T();
            instance.MdiParent = this;
            instance.Show();
        }
        else
        {
            instance.Activate();
        }
    }
    

    Usage:

    private void studentsToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        CreateMdiChildOrActivate(ref this.stu);
    }
    

    EDIT :

    If you don’t want to create a class field for each form, you can do in this way:

    Create a class dictionary field containing the open form for each form-type:

    private Dictionary<Type,Form> openForms = new Dictionary<Type,Form>();
    

    Then change the previous method to:

    public void CreateMdiChildOrActivate<T>() where T : Form, new()
    {
        Form instance;
        openForms.TryGetValue(typeof(T), out instance);
        if (instance == null || instance.IsDisposed)
        {
            instance = new T();
            openForms[typeof(T)] = instance;
            instance.MdiParent = this;
            instance.Show();
        }
        else
        {
            instance.Activate();
        }
    }
    

    Now you can call it like this:

    private void studentsToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        CreateMdiChildOrActivate<Student>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange one. Create a new form. Then add the following function
I have code in the following form in which the user may specify a
Is the following possible in SQL Server 2000? CREATE FUNCTION getItemType (@code varchar(18)) RETURNS
I created the following function to simplify a piece of particularly complex code. CREATE
I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
I have the following function: CREATE FUNCTION [dbo].[ListStockBySubCategory] ( @CategoryID varchar(10), @SubCategoryID varchar(10), @startRowIndex
I have the following scalar function in MS SQL 2005: CREATE FUNCTION [dbo].[Distance] (
This is a Function that does the following: Create a random Token with 8
I have the following function that takes a number like 5 and creates a
union members may not have destructors or constructors. So I can't template the following

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.