For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Thursday, May 22, 2014

Child form open and close problem in MDIForm C#

Child form open and close problem in MDIForm C#

===================================================================================
You have a menu item in MDIForm, after clicking on that a child form will be shown.
If you close the child form by clicking in cross "X" button which is on
right top corner, the child form will be closed.
And if you again click on MDIForm's menu item then the child form will not show.
This is because child form is disposed but that's variable is not null.
Listed below the code is the good example of that problem:
-----------------------------------------------------------------------------------
namespace MyProject
{
    public partial class AdminMDIParent : Form
    {
        Synopsis synopsis=null;
    }

    private void ShowNewForm(object sender, EventArgs e)
        {
            if (synopsis == null)
                {
                    synopsis = new Synopsis();
                    synopsis.MdiParent = this;

        }
              synopsis.Show();
              synopsis.Focus();
        }

}
-----------------------------------------------------------------------------------
The above code will not show the form in second time and rest. This is because the "synopsis"
is the object of "Synopsis" Form class. and this is the local variable of AdminMDIParent" class.
Only first time that code will work. So how to overcome this problem?
by the way, You will just use this code:
private void ShowNewForm(object sender, EventArgs e)
{
    synopsis = new Synopsis();
        synopsis.MdiParent = this;
    synopsis.Show();
        synopsis.Focus();
}
But the problem is when you use the above code and click again and again MDIForm's menu item without
closing child form; the multiple child form will be shown.
-----------------------------------------------------------------------------------

So to overcome this problem; we check its "visible" property as listed below.  Listed below code work fine.
private void ShowNewForm(object sender, EventArgs e)
{
    if (synopsis == null || synopsis.Visible==false)
    {
        synopsis = new Synopsis();
        synopsis.MdiParent = this;
    }
        synopsis.Show();
        synopsis.Focus();
}
-----------------------------------------------------------------------------------
Share:

0 comments:

Post a Comment

Multiple attribute passing in querySelectorAll

Multiple attribute passing in querySelectorAll     Here I am demonstrating code to how to pass multiple attributes in querySelectorAll. <...

Ads Inside Post

Powered by Blogger.

Arsip