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. Check if Windows Form is already loaded

Check if Windows Form is already loaded

Scheduled Pinned Locked Moved C#
questioncsharp
7 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.
  • A Offline
    A Offline
    Alan Zhao
    wrote on last edited by
    #1

    Hi guys, I guess it's a pretty easy question for you. How do you check if a Windows Form (object) is already loaded in C#? Thanks!

    H D 2 Replies Last reply
    0
    • A Alan Zhao

      Hi guys, I guess it's a pretty easy question for you. How do you check if a Windows Form (object) is already loaded in C#? Thanks!

      H Offline
      H Offline
      Hubert
      wrote on last edited by
      #2

      What exactly doyou mean? If you want to know it from within the form - just use OnLoad event. Thing goes worse if you want from out of the form. Maybe you should look for some threading topics? (ie. static semaphores set from OnLoad) h. ;)

      1 Reply Last reply
      0
      • A Alan Zhao

        Hi guys, I guess it's a pretty easy question for you. How do you check if a Windows Form (object) is already loaded in C#? Thanks!

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        What do you mean? Are you talking about an MDI Child form? In any case, your code should be written to track this information as it happens. Your code is the only thing that is going to load and instantiate the form so tracking this is pretty easy... RageInTheMachine9532

        A 1 Reply Last reply
        0
        • D Dave Kreskowiak

          What do you mean? Are you talking about an MDI Child form? In any case, your code should be written to track this information as it happens. Your code is the only thing that is going to load and instantiate the form so tracking this is pretty easy... RageInTheMachine9532

          A Offline
          A Offline
          Alan Zhao
          wrote on last edited by
          #4

          Yes, I mean the MDI child form, because it can be loaded multiple times. We just want to have one form at a time.

          D 1 Reply Last reply
          0
          • A Alan Zhao

            Yes, I mean the MDI child form, because it can be loaded multiple times. We just want to have one form at a time.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Great! When your app creates the child form, just set a global flag that says the form has been created. When you go to create the child form, just check the flag first. Done! RageInTheMachine9532

            A 1 Reply Last reply
            0
            • D Dave Kreskowiak

              Great! When your app creates the child form, just set a global flag that says the form has been created. When you go to create the child form, just check the flag first. Done! RageInTheMachine9532

              A Offline
              A Offline
              Alan Zhao
              wrote on last edited by
              #6

              Can I do this: private someForm form; if (form == null) form = new someForm(); Also, is form = null when disposed? How do I know if a form is disposed? Thank!

              D 1 Reply Last reply
              0
              • A Alan Zhao

                Can I do this: private someForm form; if (form == null) form = new someForm(); Also, is form = null when disposed? How do I know if a form is disposed? Thank!

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Actually you do both, call the Dispose() method and set the variable to null. Dispose will tell the instance to release the resources it holds while setting the variable to null will release the reference to that instance. Calling Dispose will release the resources when YOU want it to and not when the garbage collector gets around to calling Dispose for you. When you set your variable to null, the reference to that instance is dropped and, since you already called Dispose, the garbage collector only has to free the memory that the instance occupied. When you do both Dispose and null, you release resources and memory much quicker than if you waited for the GC to get around to it. RageInTheMachine9532

                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