tags:

views:

294

answers:

1

Hi. The title of the question says it all really. I have a child form which has a find form in it. I set the find form's owner to the child form like so:

private void ShowFindForm()
{
  FindForm.Show(this);
}

which then allows me to access it's properties like this:

private void FindNext()
{
  TreeNode matchingNode = ...
  ... etc
  ... etc

  OwnerForm form = this.Owner as OwnerForm;
  form.TreeView.SelectedNode = matchingNode;
}

This works perfectly fine until I shove the owner form into an MDI form, whereby the MDI form promptly takes ownership of the find form and messes it all up. How do I get around this?

UPDATE:

I can hack around this by iterating through the MDI form's MdiChildren property until I find the form I want, but this seems a bit cowboy-ish.

A: 

A simple solution to this problem is to create a public OwnerForm property on your child form like this:

public OwnerForm myOwner;

and then edit your ShowFindForm() method to this:

private void ShowFindForm()
{
    FindForm.myOwner = this;
    FindForm.Show(this);
}

and then change the second-to-last line in your child form's FindNext() method to this:

OwnerForm form = this.myOwner;

myOwner should probably actually be a private property with public get and set methods, but this is just to illustrate the principle. Basically, instead of assuming that your child form's Owner is the form whose properties you want to access (an assumption that breaks in the MDI world, as you've found), you're explicitly creating a reference to the form you want.

As a side note, many would consider your code sample to be a violation of the OOP principle of encapsulation, since you're manipulating one form's controls from another form. I've seen worse crimes against humanity, myself.

MusiGenesis
He he. That would make sense really. I'm going to put that down to the fact it's late, I've been programming for 6 hours non-stop and I've not had a can of Red Bull. Either that or I'm just not that clever! Cheers mate.
woodstock