Click here to Skip to main content
14,328,470 members
Rate this:
Please Sign up or sign in to vote.
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:

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.
webmail123 27-Mar-18 9:59am
   
Thanks for the explanation. I will keep that in mind and look for another way of implementing the same.
j snooze 26-Mar-18 17:44pm
   
what is itemData? Why not set itemdata to null when the child form is closed?
webmail123 27-Mar-18 8:38am
   
Sorry. I forgot to mention that in code. I updated the code now. ItemData is object of frmItemData. I set itemdata=null at frmItemData close event. But its still the same.
BillWoodruff 27-Mar-18 9:20am
   
Your code, and your statements about your code do not make sense. Try and explain in more detail.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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
}
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100