New to C# - Accessing Form Methods

  • Thread starter Thread starter Mal Ball
  • Start date Start date
M

Mal Ball

I have an MDI main form which has a method on it to
refresh a tree. When I add a customer record using one of
my Child forms I want to be able to refresh the tree on
the main form using the method.

The method is declared as:
public void RefreshTree()

If I try and add this.ParentForm.RefreshTree() to my
child form it does not work.

I am sure I am making a fundamental error here, could
someone let me know what it is please?
 
If I try and add this.ParentForm.RefreshTree() to my
child form it does not work.

Since the RefreshTree method is only available on your form class, and
not from the base System.Windows.Forms.Form class, you have to cast
the reference to your own form type to call the method

((YourMDIForm)this.ParentForm).RefreshTree();



Mattias
 
I'm assuming that your child form is in a different class
then the parent. If that is the case I just had a
similar issue with textboxes in different forms/classes.
When you call the method in the child form you need to
send a reference of ParentForm and then call
ParentForm.RefreshTree() from the child.
I hope this helps.
-Tim
 
Thanks very much for this detail, I just had to change
the method name and MDI Form name and this worked a treat.

Mal Ball
 
Back
Top