Re: How to call a function from another form? by Marc
Marc
Mon Mar 13 09:50:04 CST 2006
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
"Nestor" <n3570r@yahoo.com> wrote in message
news:OL$5P2qRGHA.1780@TK2MSFTNGP12.phx.gbl...
> 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?
>
>
> "pop.nagarro.com" <viveks@nagarro.com> wrote in message
> news:e$g$QvlRGHA.5900@tk2msftngp13.phx.gbl...
>>
>> "Nestor" <n3570r@yahoo.com> wrote in message
>> news:uDsr%232eRGHA.5092@TK2MSFTNGP11.phx.gbl...
>>> 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.
>>>
>>>
>>>
>>
>> Make those functions public, create an instance of the form and call
>> through that instance.
>>
>> Vivek
>>
>
>