Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Accessing methods on a parent form from a new form where the parents can be different

$
0
0

I have an application that in the main program  will run one of two forms (Form_A or Form_B) depending on a certain parameter.  Form_A and Form_B each have a public method, method_doSomething.

Pressing a button in Form_A OR Form_B will open a new form, Form_C.  

In form C I need to run the method, method_doSomething in the calling form.

Previously I had only Form_A and Form_C had no problem accessing the method with

FormA.method_doSomething();

However, now I have the possibility of two forms and cannot explicitly reference FormA.method_doSomething since the parent class is now different.

There is another question exactly the same in these forums but the answers do not work.

Here was one answer:

One way of doing that can be to set the `ParentForm` property before opening `FormC` like:

FormC formC =newFormC();
formC.ParentForm=this;
formC.Show();

Then In `FormC` you can check the `ParentForm` and call the method accordingly. 

if(this.ParentForm!=null&&this.ParentFormisFormA){((FormA)this.ParentForm).method_doSomething();}if(this.ParentForm!=null&&this.ParentFormisFormB){((FormB)this.ParentForm).method_doSomething();}

However, the line 

formC.ParentForm=this;

Throws an error " property cannot be assigned to : it is read only.

Can someone help out please?


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>