|
-
Dec 15th, 2006, 04:03 AM
#1
Thread Starter
Lively Member
Call function in FormParent?
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.
!Have fun!

-
Dec 20th, 2006, 12:17 AM
#2
Re: Call function in FormParent?
How are you trying to do it at the moment?
-
Dec 20th, 2006, 04:16 AM
#3
Fanatic Member
Re: Call function in FormParent?
 Originally Posted by fifo
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.
Make your function C a static then in formB
You can
FormA.FunctionC();
-
Dec 20th, 2006, 05:49 AM
#4
Re: Call function in FormParent?
 Originally Posted by popskie
Make your function C a static then in formB
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.
-
Dec 20th, 2006, 11:09 PM
#5
Thread Starter
Lively Member
Re: Call function in FormParent?
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.
Static is good in this case.
Do you have another ways? show me
!Have fun!

-
Dec 20th, 2006, 11:30 PM
#6
Re: Call function in FormParent?
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:
Code:
FormB f = new FormB();
f.ShowDialog(this);
In FormB:
Code:
FormA f = (FormA)this.Owner;
f.SomeMethod();
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.
-
Dec 20th, 2006, 11:38 PM
#7
Fanatic Member
Re: Call function in FormParent?
 Originally Posted by jmcilhinney
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:
Code:
FormB f = new FormB();
f.ShowDialog(this);
In FormB:
Code:
FormA f = (FormA)this.Owner;
f.SomeMethod();
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.
In case that you want to use the .show() method
VB Code:
Form2 f = new Form2();
this.AddOwnedForm(f);
f.Show();
In formB you do the same what JM said.
-
Dec 20th, 2006, 11:46 PM
#8
Re: Call function in FormParent?
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.
-
Dec 21st, 2006, 04:52 AM
#9
Thread Starter
Lively Member
Re: Call function in FormParent?
thanks, its very helpful for me.
I wonder whether we have another better solution.
!Have fun!

-
Dec 21st, 2006, 07:51 AM
#10
Re: Call function in FormParent?
 Originally Posted by fifo
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|