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. Visual Basic
  4. Keeping track of a form

Keeping track of a form

Scheduled Pinned Locked Moved Visual Basic
6 Posts 5 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I have a form and I want to make it so only one instance of that form is opened. I can do this with a counter variable in my form class so when it is loaded it incremenets and if they go to file->exit it decriments. But if thye hit the X button in the upper right, the form is closed and my counter vraiable stays at one. I need a way to make it so however they close it my counter is updated accordingly.

    A 1 Reply Last reply
    0
    • L Lost User

      I have a form and I want to make it so only one instance of that form is opened. I can do this with a counter variable in my form class so when it is loaded it incremenets and if they go to file->exit it decriments. But if thye hit the X button in the upper right, the form is closed and my counter vraiable stays at one. I need a way to make it so however they close it my counter is updated accordingly.

      A Offline
      A Offline
      Andy Smith
      wrote on last edited by
      #2

      I would suggest that you use the Singleton pattern instead of tracking counters.

      T 1 Reply Last reply
      0
      • A Andy Smith

        I would suggest that you use the Singleton pattern instead of tracking counters.

        T Offline
        T Offline
        Tantalus
        wrote on last edited by
        #3

        A singleton pattern is? -- Remember your are but a lowly hair on my s*r*t*m

        A J 2 Replies Last reply
        0
        • T Tantalus

          A singleton pattern is? -- Remember your are but a lowly hair on my s*r*t*m

          A Offline
          A Offline
          Andy Smith
          wrote on last edited by
          #4

          the following is similar to how the vb6 upgrade wizard does it. it's not a true singleton pattern, because it allows for more than one to be created over the lifetime of the app, but is close enough in that it never allows more than one at a time. add this code to your form, substituting "Form1" for the type of your form. Private Shared m_SingleInstance As Form1 Private Shared m_InitializingSingleInstance As Boolean Public Shared Property SingleInstance() As Form1 Get If m_SingleInstance Is Nothing OrElse m_SingleInstance.IsDisposed Then m_InitializingSingleInstance = True m_SingleInstance = New Form1() m_InitializingSingleInstance = False End If SingleInstance= m_SingleInstance End Get End Property now when you want to create or reference your one form, use Form1.SingleInstance instead of creating or showing it manually.

          1 Reply Last reply
          0
          • T Tantalus

            A singleton pattern is? -- Remember your are but a lowly hair on my s*r*t*m

            J Offline
            J Offline
            jkgh
            wrote on last edited by
            #5

            http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/singletondespatt.asp There are a couple of others there too: factory and .. er.. I forgot. Alice thought that running very fast for a long time would get you to somewhere else. " A very slow kind of country!" said the queen. "Now, here , you see, it takes all the running you can do, to keep in the same place".

            realJSOPR 1 Reply Last reply
            0
            • J jkgh

              http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/singletondespatt.asp There are a couple of others there too: factory and .. er.. I forgot. Alice thought that running very fast for a long time would get you to somewhere else. " A very slow kind of country!" said the queen. "Now, here , you see, it takes all the running you can do, to keep in the same place".

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              Just curious - why did you change your nick to jkgh? "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 "Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied." - Alex, 13 June 2002 Please review the Legal Disclaimer in my bio.

              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