Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Calling a method from a form in another form [modified]

Calling a method from a form in another form [modified]

Scheduled Pinned Locked Moved C#
helpregextutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    motojojo
    wrote on last edited by
    #1

    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

    C M M 3 Replies Last reply
    0
    • M motojojo

      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

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M motojojo

        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

        M Offline
        M Offline
        Mark T
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M motojojo

          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

          M Offline
          M Offline
          motojojo
          wrote on last edited by
          #4

          thank you for your feedback guys :) I've worked around it

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups