How to stop two forms to open simultaneously

Experts
I have a question .
As you can see that in the attached image as I click the submenu Sales Entries then that form is opening but after that if I click the Dealer Entries then this form goes behind the Sales Entries.

Similarly if at first I click the Dealer Entries then that form opens but as I click the Sales Entries form then the Sales Entries form goes below the Dealer Entries form.

But this is not expected.
My question is that how to prevent one form to get opened unless another form is not closed.

For your information I would like to tell you that I have used here Dealer Entries form and Sales Entries form these are border less form and the code I used is as


  private void salesEntriesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormSalesEntry fse = new FormSalesEntry();
            fse.TopLevel = false;
            WorkingPanel.Controls.Add(fse);
            fse.Visible = true;
           

        }

        private void dealerEntriesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form_DealerInfo fdi = new Form_DealerInfo();
            fdi.TopLevel = false;
            WorkingPanel.Controls.Add(fdi);
            fdi.Visible = true;
        }
My question is  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDManagementSoftware
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void salesEntriesToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void salesEntriesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormSalesEntry fse = new FormSalesEntry();
            fse.TopLevel = false;
            WorkingPanel.Controls.Add(fse);
            fse.Visible = true;
            

        }

        private void dealerEntriesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form_DealerInfo fdi = new Form_DealerInfo();
            fdi.TopLevel = false;
            WorkingPanel.Controls.Add(fdi);
            fdi.Visible = true;
        }
    }
}

Open in new window

error.JPG
ANINDYAAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
How are you closing the forms?

As I said before, you can simply prevent the opening of another form if there is already one open in the panel:

        private void salesEntriesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (WorkingPanel.Controls.Count == 0)
            {
                FormSalesEntry fse = new FormSalesEntry();
                fse.TopLevel = false;
                WorkingPanel.Controls.Add(fse);
                fse.Visible = true;
            }
        }
0
 
SriVaddadiCommented:
IOW,  you want to make the form as modal. does it sound right?  Use ShowDialog method of the form instead of setting visible property

you could do something like this

FormSalesEntry fse = new FormSalesEntry();
            fse.TopLevel = false;
            WorkingPanel.Controls.Add(fse);
fse.ShowDialog(this);



0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
(not sure you can use ShowDialog() when the form is not TopLevel and is being displayed in a Panel...haven't tried it)

You could either:
(1) Check the WorkingPanel to see if it already has a form loaded:
    if (!WorkingPanel.Controls.Count == 0)
    {
       // load the new form up, the above check may need to be more complex depending on what is in WorkingPanel
    }
(2) Disable the menu option until the form is closed by subscribing to the FormClosed() event and re-enabling when the menu when it fires.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
ANINDYAAuthor Commented:
Expert SriVaddadi
Thanks for your suggestion but sir it not working sir.
I tried.
It is showing error.
Thanking you
error.JPG
0
 
ANINDYAAuthor Commented:
Expert Idle_Mind
I hope you can remember me.Sir it is your code only sir as you had suggested me that I am using in the total project.
I am extremely elated to see the result.
But I am facing a problem as I depicted in this question .
As you can see there are
Sales entries ,dealer entries,inventory,customer,payment entries. So what is happening is all the forms are piled on another.
Sir it is nice to see you only again .
I did not know how to contact to you.
Sir I can not disable the menu option as otherwise  the customer will not be able to understand  them..The end user might think those are options for me or I am unable to use them..

Sir your 1st option I tried but it is becoming too heavy for me to control the code.
Can you suggest a simple option.sir.
Thanking you Idle_Mind for answer ,
Anindya Chatterjee
Bangalore
India


0
 
ANINDYAAuthor Commented:
Again Thanks Idle_Mind for your timely answer.
I am sorry for not responding in time as I was not in good health.
Thanking you
Anindya Chatterjee
Bangalore
India
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.