0

First of all, I want to clarify that similar questions like this, is already asked in stackoverflow but none of those solved my problem. So, before you mark my questions as a duplicate, please understand my problem first.

I have two form named frmNewCustomer and frmCustomers. Inside frmCustomers I have a button control named btnNewCustomer that opens frmNewCustomer whenever I click the button. These two forms are MdiChildren of another form named WMS. Now, I don't want to open a form if it is already opened. For that what I have done is this (Code for frmCustomers).

namespace WMS.Presentation
{
    public partial class frmCustomers : Form
    {

        Form newCustomer = new frmNewCustomer();

        public frmCustomers()
        {
            InitializeComponent();
        }


        private void btnNewCustomer_Click(object sender, EventArgs e)
        {
            if (newCustomer.IsDisposed)
                newCustomer = new frmNewCustomer();

            newCustomer.MdiParent = this.MdiParent;
            newCustomer.Show();
            newCustomer.BringToFront();
        }
    }
}

Now suppose, I opened the form frmCustomers from the main form (Form1) menu. After that I clicked the button btnNewCustomer. So, I have to form opened now inside Form1. If I again click on the file menu of Form1 and try to open frmCustomers it doesn't open a new form, it just focus on the opened instance of frmCustomers which is fine. This way I can focus on frmNewCustomers too.

Now, If I close frmCustomers while frmNewCustomer is opened then again open frmCustomers and try to focus on the opened frmNewCustomers by clicking btnNewCustomer it doesn't work. It opens a new frmNewCustomer which I don't want. I just simply want to focus on the opened instance of frmNewCustomer. What can I do to solve my problem? I am enclosing aenter image description here screenshot so you may have better understanding of my problem.

Rubel Hosen
  • 199
  • 4
  • 20
  • because your instance of frmCustomers is closed (not in memory). And then you are making new instance of frmCustomers so it will open new customer form one more time. – Gaurang Dave Mar 06 '18 at 02:47
  • Why are you creating newCustomer form inside frmCustomers ? If that is the requirement then newCustomer 's parent should be frmCustomer. You should close all the instance properly with their children forms. – Gaurang Dave Mar 06 '18 at 02:47
  • Yes, This is the requirement. So, I should also close `frmCustomers` if I want to close `frmNewCustomer`? – Rubel Hosen Mar 06 '18 at 03:05
  • check my answer. newcustomer will be independent – Gaurang Dave Mar 06 '18 at 03:16

3 Answers3

0

There are many proactive and reactive ways of doing this

However, you can use something like this, this will reactivate the Form if its open, or create it if its not.

public void OpenExclusive<T>(Form parent) where T : Form, new()
{
    foreach (Form form in Application.OpenForms)
    {
        if (form.GetType() == typeof(T))
        {
            form.Activate();
            return;
        }
    }

    Form newForm = new T();
    newForm.MdiParent = parent;
    newForm.Show();
}

Usage

OpenExclusive<MyForm>(this);

Note : written on SO and totally untested

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

Try this one to close the newCustomer form while closing frmCustomers form.

namespace WMS.Presentation
{
    public partial class frmCustomers : Form
    {

        Form newCustomer = new frmNewCustomer();

        public frmCustomers()
        {
            InitializeComponent();

            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(FormClosing);
        }


        private void btnNewCustomer_Click(object sender, EventArgs e)
        {
            if (newCustomer.IsDisposed)
                newCustomer = new frmNewCustomer();

            newCustomer.MdiParent = this.MdiParent;
            newCustomer.Show();
            newCustomer.BringToFront();
        }

        private void FormClosing(object sender, EventArgs e)
        {
            if (!newCustomer.IsDisposed)
                newCustomer.Close();
        }
    }
}

NOTE: Wrote on SO. Did not test it. Please make necessary changes.

Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34
0

Write a function that finds the open form by its classname, and if found opens it, and restores it when minimized.
When the function retuns false, the form will not be opened yet and you can open for the first time.
This works well with my in my winforms application

public bool CheckIfFormIsAlreadyOpen(string className)
{
    bool Result = false;
    className = className.ToUpper();

    int i = 0;
    while ((i < Application.OpenForms.Count) && (Application.OpenForms[i].Name.ToUpper() != className))
    {
        i++;
    }
    if (i < Application.OpenForms.Count)
    {
        Application.OpenForms[i].Show();
        Application.OpenForms[i].BringToFront();
        if (Application.OpenForms[i].WindowState == FormWindowState.Minimized)
        {
           ShowWindowAsync(Application.OpenForms[i].Handle, 9); // 9 = SW_RESTORE
        }

        Result = true;
    }

    return Result;
}

Usage :

if (!CheckIfFormIsAlreadyOpen("frmCustomers"))
{
    frmCustomers form = new frmCustomers();
    form.MdiParent = this.MdiParent;
    form.Show();
}
GuidoG
  • 11,359
  • 6
  • 44
  • 79