Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1868
  • Last Modified:

Check if child form is open does not work

Hi
I have a method in my MDI parent form witch checks if a child form is already open:

public bool KidsOpen(Form FormName)
        {
            Form[] childform = this.MainDockingManager.MdiChildren;
            if (this.MainDockingManager.MdiChildren.Length == 0)
            {
                return false;
            }
            else
            {
                foreach (Form Myform in childform)
                {
                    if (Myform.Name == FormName.Name)
                    {
                        Myform.BringToFront();
                        return true;
                    }
                }
                return false;
            }
        }

this works when i open a child form from menu in the parent with this method..

case "Edit user":
                        {
                            EditUser form = new EditUser();
                            form.Text = "Edit user ";
                            form.MdiParent = this;
                            if (!KidsOpen(form))
                            {
                                form.Show();
                            }
                            else
                            {
                                SetActiveForm(form);
                            }
                            break;
                        }
Now, I want to open a child form from another child form..there i user following method..

private void EditUserFromGrid(string username)
        {
            MainAdmin main = new MainAdmin();
            Form mdiForm = this.ParentForm;
            if (mdiForm.ActiveMdiChild != null)
            {
                if (username != null)
                {
                    EditUser form = new EditUser(username);
                    form.Text = "Edit user";
                    form.MdiParent = mdiForm;
                    if (!main.KidsOpen(form))
                    {
                        form.Show();
                    }
                }
            }
        }

this opens the form inside the parent but does not find the already open children...
The MdiChildren is null and Length is also null

  Form[] childform = this.MainDockingManager.MdiChildren;
            if (this.MainDockingManager.MdiChildren.Length == 0)
but it works when i call the method from the MDI parent.

Regards
0
ize_man
Asked:
ize_man
  • 2
1 Solution
 
RimvisCommented:
if (!main.KidsOpen(form))

shouldn't it be

if (!mdiForm.KidsOpen(form))
0
 
ize_manAuthor Commented:
I tried that but it did not work... :-( so I had to add this to my method to be able to access KidsOpen method

MainAdmin main = new MainAdmin();

this one does find the parent form for the child i am calling from

Form mdiForm = this.ParentForm;

this was my first approach...it looked obvious to me...but it doe's not work. (I wonder why!!) even if it seems like the mdiForm and main has exactly same values I am not able to get KidsOpen method by using mdiForm...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Instead of:

    Form mdiForm = this.ParentForm;

Try:

    yourMdiParentTypeHere mdiForm = (yourMdiParentTypeHere)this.MdiParent;

Then as Rimvis said...

Change:

    if (!main.KidsOpen(form))

To:

    if (!mdiForm.KidsOpen(form))

0
 
ize_manAuthor Commented:
This does not work

yourMdiParentTypeHere = MainAdmin (That's what you mean??)

MainAdmin main = (MainAdmin)this.MdiParent;

main is null in debug mode....so I tried this..

MainAdmin main2 = (MainAdmin)this.ParentForm;

then i run the rest..

if(!main2.KidsOpen(form))
{
....

and it worked :-) ..so I believe that problem is solved...
Thanks for the help
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now