Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2006

Tip: Looking for answers? Try searching our database.

How to call a function from another form?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nestor - 12 Mar 2006 16:59 GMT
Hello, I'm trying to call a function from another form.... If I have 3
forms, A, B and C. A calls form B and C, and I want to call a function in C
from form B. Is there anyway to do this?

Any advise will be appreciated.

Thanks.
pop.nagarro.com - 13 Mar 2006 06:09 GMT
> Hello, I'm trying to call a function from another form.... If I have 3
> forms, A, B and C. A calls form B and C, and I want to call a function in
[quoted text clipped - 3 lines]
>
> Thanks.

Make those functions public, create an instance of the form and call through
that instance.

Vivek
Nestor - 13 Mar 2006 15:52 GMT
The problem is I don't want to create another instance of the other form C
from form B. Basically Form A created new instances of Form B and Form C. If
I were to create another new instance of Form C from Form B, I will end up
'popping' another new UI instance of Form C from C, meaning I will have 2
forms showing which is not what I wanted.

However I want to access some public functions of Form C from Form B. How do
I go about doing that? thru delegates and events?

>> Hello, I'm trying to call a function from another form.... If I have 3
>> forms, A, B and C. A calls form B and C, and I want to call a function in
[quoted text clipped - 8 lines]
>
> Vivek
Sericinus hunter - 13 Mar 2006 16:31 GMT
When creating form B, you can pass an reference to form C to it, where
it will be held for later use. Will this do?

> The problem is I don't want to create another instance of the other form C
> from form B. Basically Form A created new instances of Form B and Form C. If
[quoted text clipped - 17 lines]
>>
>> Vivek
Marc Gravell - 13 Mar 2006 16:50 GMT
As Sericinus hunter points out, you can pass an instance of B to C - i.e. by
creating a public (or internal) property on C that B assigns itself (this)
to after creating B and prior to showing it. This can also be done as a
parameter to the ctor (of C) if you want, but be sure to leave the default
(parameterless) ctor, otherwise the designer can throw toys out of the pram.

*however*; I would first ask (of myself, so somewhat rhetorical) what the
nature of the method is; if they are highly specific and have a demanding
interface (i.e. by their nature requiring a number of highly specific
parameters) I might go down the above route. If, however, it is more about
notifications between the forms, then I would do this as an event on C (i.e.
CustomerSelected, PrintRequested, UniverseEnding etc), which B would
subscribe to prior to showing.

I would also check with myself whether the functions I want to call are
actually related to the UI; it might be that they exist better as public
functions of a helper class (i.e. FormatCustomerForDisplay, etc) that exist
in a separate class accessed (separately) from B and C.

All of which may-or-may-not help you pick a way forward...

Marc

> The problem is I don't want to create another instance of the other form C
> from form B. Basically Form A created new instances of Form B and Form C.
[quoted text clipped - 17 lines]
>>
>> Vivek
Stoitcho Goutsev (100) - 13 Mar 2006 15:31 GMT
Nestor,

In order class memeber to be visible outside the class (without inhertance)
they have to be either public or internal. In the latter case the both
caller and callee needs to be compiler in the same assembly. Keep in mind
also that if you declare something public then the enclosing entity needs to
have the same or less restrictive declaration that is if you declare a
method internal the declaring type needs to be internal or public. If the
declaring type is declared nested in some other type the outer type needs to
have the right visibility also.

Signature

HTH
Stoitcho Goutsev (100)

> Hello, I'm trying to call a function from another form.... If I have 3
> forms, A, B and C. A calls form B and C, and I want to call a function in
[quoted text clipped - 3 lines]
>
> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.