Calling a method from a form in another form [modified]
-
Hi, I was wondering how to access a method in a form from another form. I know that this implies using the 'static' modifier, however, this seems to be very troublesome. I was thinking there should be a way to access the instance of a form at realtime to use one of it's public methods.. (The form of which the method I want to call happens to be the main form of the application) Does anyone know how to do this? I'm stuck :) Actually I was considering using the Observer Pattern for solving this problem, however I expect there should be a much more straight-forward solution..? With kind regards, Johan Vandewalle -- modified at 6:06 Wednesday 21st February, 2007
-
Hi, I was wondering how to access a method in a form from another form. I know that this implies using the 'static' modifier, however, this seems to be very troublesome. I was thinking there should be a way to access the instance of a form at realtime to use one of it's public methods.. (The form of which the method I want to call happens to be the main form of the application) Does anyone know how to do this? I'm stuck :) Actually I was considering using the Observer Pattern for solving this problem, however I expect there should be a much more straight-forward solution..? With kind regards, Johan Vandewalle -- modified at 6:06 Wednesday 21st February, 2007
motojojo wrote:
I know that this implies using the 'static' modifier, however, this seems to be very troublesome.
No, it doesn't.
motojojo wrote:
Actually I was considering using the Observer Pattern for solving this problem, however I expect there should be a much more straight-forward solution..?
The observer pattern is a good solution. You can get an example in this article about passing values between forms[^].
Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos
-
Hi, I was wondering how to access a method in a form from another form. I know that this implies using the 'static' modifier, however, this seems to be very troublesome. I was thinking there should be a way to access the instance of a form at realtime to use one of it's public methods.. (The form of which the method I want to call happens to be the main form of the application) Does anyone know how to do this? I'm stuck :) Actually I was considering using the Observer Pattern for solving this problem, however I expect there should be a much more straight-forward solution..? With kind regards, Johan Vandewalle -- modified at 6:06 Wednesday 21st February, 2007
I hope someone can come up with a better method, but here is how I solve that problem. In the second form, make a new function (or put it in the form's constructor) which accepts a form as its argument and stores it in a local variable (perhaps called mainform). From Main(), call this function and pass the main form. Now, to call something withing the main form's class just use: mainform.mymethod(); Mark
-
Hi, I was wondering how to access a method in a form from another form. I know that this implies using the 'static' modifier, however, this seems to be very troublesome. I was thinking there should be a way to access the instance of a form at realtime to use one of it's public methods.. (The form of which the method I want to call happens to be the main form of the application) Does anyone know how to do this? I'm stuck :) Actually I was considering using the Observer Pattern for solving this problem, however I expect there should be a much more straight-forward solution..? With kind regards, Johan Vandewalle -- modified at 6:06 Wednesday 21st February, 2007