> 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.
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