Hi,
I made a form A with a public function C in this, and another form B.
I call formB.showDialog(owner A) in form A, and then in form B, I want to use function C in form A, BUT I can't call this function.
I dont know why? Show me how.
Printable View
Hi,
I made a form A with a public function C in this, and another form B.
I call formB.showDialog(owner A) in form A, and then in form B, I want to use function C in form A, BUT I can't call this function.
I dont know why? Show me how.
How are you trying to do it at the moment?
Make your function C a static then in formBQuote:
Originally Posted by fifo
You can
FormA.FunctionC();
You don't declare members static just for convenience. If it's appropriate that the member is static then you declare it static. In this case it is NOT appropriate.Quote:
Originally Posted by popskie
Static is good in this case.Quote:
You don't declare members static just for convenience. If it's appropriate that the member is static then you declare it static. In this case it is NOT appropriate.
Do you have another ways? show me
Declaring the method static may work but I doubt that it is "good". If you hadn't already declare dthe method static then I very much doubt that it is appropriate to declare it static now. Like I said, the "static" key word doesn't exist just for convenience. It has a specific purpose and if that purpose is not served then it should not be used.
The dialogue has a reference to the calling form in its Owner property. You need to cast the Owner as the appropriate type to access the members of that type. In FormA:In FormB:Code:FormB f = new FormB();
f.ShowDialog(this);
Having said that, it should be a rare thing that a dialogue should need to call a method of its caller. Under almost all circumstances the caller should pass all the data to the dialogue via its constructor or properties before calling ShowDialog, then it should retrieve all the data it needs from the dialogue via properties or methods after ShowDialog returns. In the vast majority of cases the dialogue shouldn't even need to know that the caller exists.Code:FormA f = (FormA)this.Owner;
f.SomeMethod();
In case that you want to use the .show() methodQuote:
Originally Posted by jmcilhinney
VB Code:
Form2 f = new Form2(); this.AddOwnedForm(f); f.Show();
In formB you do the same what JM said.
You should note that creating an owned form does more than just give the owned a reference to the owner. It also creates a true modeless dialogue, much like the VS Find & Replace dialogue. An owned form remains on top of its owner without restricting access to it, plus it will be minimised, restored and closed whenever the owner is.
thanks, its very helpful for me.
I wonder whether we have another better solution.
Sacrilege! A better solution than mine? ;) Are you trying to shatter my illusions? :)Quote:
Originally Posted by fifo