Click here to Skip to main content
15,943,089 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I know this problem or issue is solved previously but I am having an issue in that. So please help me.
I have MDI form and 3-4 child forms. I want to disable to open the multiple child forms. I have the coding from previous post. And it work perfectly. But when I close that form and try to open the same form again using the same menu, it won't open.

What I have tried:

C#
frmItemData itemData;
private void mnuSearchItem_Click(object sender, EventArgs e)
{
    if (itemData == null)
        {
            itemData = new frmItemData();
            itemData.MdiParent = this;
            itemData.FormClosed += ItemData_FormClosed;
            itemData.Show();
        }
}


By this code, it avoid to open multiple child forms. But the problem is, when I open this form, close it and when I try to again open the same form by clicking on menu button then form is not opening. I debugged the code and after first time, itemData always returns Not Null.

So can anyone help me with this one?
Posted
Updated 27-Mar-18 2:36am
v2
Comments
#realJSOP 26-Mar-18 16:52pm    
If you don't want multiple MDI forms to be able to be opened, why is your app an MDI app?
webmail123 27-Mar-18 8:38am    
So if you observe the title as well as code, I don't have multiple MDI forms.
Dave Kreskowiak 26-Mar-18 17:10pm    
Do you have any idea what MDI is? Why are you using it?
webmail123 27-Mar-18 8:37am    
Yes I have idea about MDI forms. I need those forms to be treated other forms as a child forms for ease of use from user perspective.
Dave Kreskowiak 27-Mar-18 9:04am    
I beg to differ. MDI stands for "Multiple Document Interface". Think Notepad, but being able to open multiple text files in the same window. All of those editor child windows are the same class, editing the same type of document. Now, it's possible to use multiple editor classes as child windows, but most people think they have to use MDI to be able to open multiple windows that have nothing to do with editing anything as MDI children. You don't. MDI should very rarely ever be used anymore.

Now, if you've got a legit reason to use MDI, great. Have at it. "Ease of use" is not a legit reason. "How" and "why" you're using it are the questions you should be asking.

Limiting an MDI child window to just a single instance flies in the face of using MDI.

1 solution

I would do something like this....

    private void mnuSearchItem_Click(object sender, EventArgs e)
    {
        Boolean IsFormShown = false;
        foreach (Form ChildForm in this.MdiChildren)
        {
           if (ChildForm.Name == "frmItemData")
            {
                IsFormShown = true;
                ChildForm.Focus();
            }  // End if
        } // end for
        if (!IsFormShown)
        {
        Cursor.Current = Cursors.WaitCursor;
        itemData = new frmItemData();
        itemData.MdiParent = this;
        itemData.Font = this.Font;
        itemData.Show();
        Cursor.Current = Cursors.Default;
    }  // End If
}
 
Share this answer
 
Comments
webmail123 27-Mar-18 10:00am    
Thank you for the solution. I will try and let you know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900