Results 1 to 10 of 10

Thread: Call function in FormParent?

  1. #1

    Thread Starter
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Call function in FormParent?

    How are you trying to do it at the moment?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: Call function in FormParent?

    Quote 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();

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Call function in FormParent?

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    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!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: Call function in FormParent?

    Quote 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:
    1. Form2 f = new Form2();
    2.             this.AddOwnedForm(f);
    3.             f.Show();

    In formB you do the same what JM said.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    Re: Call function in FormParent?

    thanks, its very helpful for me.

    I wonder whether we have another better solution.
    !Have fun!

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Call function in FormParent?

    Quote 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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width