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. Single Instance of a Form

Single Instance of a Form

Scheduled Pinned Locked Moved C#
oophelpquestion
5 Posts 2 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.
  • P Offline
    P Offline
    PHDENG81
    wrote on last edited by
    #1

    I have a Main Application that uses viusal inheritance to call 30 or so other forms. Therefore, I have about 30 dll's that I reference that represent each form. I want to be able to instantiate each form only once. If the form already exists, I want to bring it to focus. I have done this using the following code: private static Namespace.Class Form = null; button1_ClickEvent() { if ( Form == null || Form.IsDisposed == true) { Form = new Namespace.Class(); Form.Show(); } Form.BringToFront(); } This is where my problem arises. I have to write this code for each form I "plan" on instantiating. How could I roll this up into a function or class that I could "call" for each form? For instance: private Instance(Form, Namespace, Class) { if ( Form == null || Form.IsDisposed == true) { Form = new Namespace.Class(); Form.Show(); } Form.BringToFront(); } I would then pass all of the respective variables. Thanks in advance

    S 2 Replies Last reply
    0
    • P PHDENG81

      I have a Main Application that uses viusal inheritance to call 30 or so other forms. Therefore, I have about 30 dll's that I reference that represent each form. I want to be able to instantiate each form only once. If the form already exists, I want to bring it to focus. I have done this using the following code: private static Namespace.Class Form = null; button1_ClickEvent() { if ( Form == null || Form.IsDisposed == true) { Form = new Namespace.Class(); Form.Show(); } Form.BringToFront(); } This is where my problem arises. I have to write this code for each form I "plan" on instantiating. How could I roll this up into a function or class that I could "call" for each form? For instance: private Instance(Form, Namespace, Class) { if ( Form == null || Form.IsDisposed == true) { Form = new Namespace.Class(); Form.Show(); } Form.BringToFront(); } I would then pass all of the respective variables. Thanks in advance

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      [Message Deleted]

      1 Reply Last reply
      0
      • P PHDENG81

        I have a Main Application that uses viusal inheritance to call 30 or so other forms. Therefore, I have about 30 dll's that I reference that represent each form. I want to be able to instantiate each form only once. If the form already exists, I want to bring it to focus. I have done this using the following code: private static Namespace.Class Form = null; button1_ClickEvent() { if ( Form == null || Form.IsDisposed == true) { Form = new Namespace.Class(); Form.Show(); } Form.BringToFront(); } This is where my problem arises. I have to write this code for each form I "plan" on instantiating. How could I roll this up into a function or class that I could "call" for each form? For instance: private Instance(Form, Namespace, Class) { if ( Form == null || Form.IsDisposed == true) { Form = new Namespace.Class(); Form.Show(); } Form.BringToFront(); } I would then pass all of the respective variables. Thanks in advance

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        You can do that using reflection. But you'd then need to maintain a hashtable of sorts to keep track of which forms are currently visible. If you can do that, then the following code will work.

        private Form CreateIfNeeded(Type formType)
        {
        Form f = (Form) formMap[formType]; //formMap maintains forms which are currently visible.
        if (f == null)
        {
        f = (Form)Activator.CreateInstance(formType);
        f.Show();
        }
        f.BringToFront();
        }

        Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        P 1 Reply Last reply
        0
        • S S Senthil Kumar

          You can do that using reflection. But you'd then need to maintain a hashtable of sorts to keep track of which forms are currently visible. If you can do that, then the following code will work.

          private Form CreateIfNeeded(Type formType)
          {
          Form f = (Form) formMap[formType]; //formMap maintains forms which are currently visible.
          if (f == null)
          {
          f = (Form)Activator.CreateInstance(formType);
          f.Show();
          }
          f.BringToFront();
          }

          Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          P Offline
          P Offline
          PHDENG81
          wrote on last edited by
          #4

          Thanks for the response. Why would I need to use the HashTable? The way I had it setup didn't use one. I simply checked whether or not the form was active or not. It seems like there would be an easier way of doing this. I really appreciate your help.

          S 1 Reply Last reply
          0
          • P PHDENG81

            Thanks for the response. Why would I need to use the HashTable? The way I had it setup didn't use one. I simply checked whether or not the form was active or not. It seems like there would be an easier way of doing this. I really appreciate your help.

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            That's because you wanted a generic method to do the job. If you don't have a hashtable, how will you know if a form a has been instantiated or not? In other words, from where will you get the instance of the form to check if it's null or disposed? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            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