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. How to only open one instance of a window form from parent form

How to only open one instance of a window form from parent form

Scheduled Pinned Locked Moved C#
questiontutorial
11 Posts 6 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.
  • D Deques

    I have tried using the mdi method, but found out that it wasnt what I wanted. It opens a form within the parent form, which isnt the way I want What I want to do is to open a form from a parent form. If I use form.Show(); I can go back to the parent form and open the same form again. That I want not possible. If I use form.ShowDialog(); I cant go back to the parent form, which is not what I want either. What I want to do is to be able to go back to the parent form and open other forms, but not open the same form twice. How do I do that?

    P Offline
    P Offline
    Pankaj Joshi
    wrote on last edited by
    #2

    For that you have to create a globally accessible class. In that just put bool variable for each form like : IsForm1Open=false; IsForm2Open=false; IsForm3Open=false; Before opening any Form from the MDI first check this variable. And on the each form's load event make that variable true. and in the dispose method make this false. ;)

    Regards Pankaj Joshi

    P 1 Reply Last reply
    0
    • P Pankaj Joshi

      For that you have to create a globally accessible class. In that just put bool variable for each form like : IsForm1Open=false; IsForm2Open=false; IsForm3Open=false; Before opening any Form from the MDI first check this variable. And on the each form's load event make that variable true. and in the dispose method make this false. ;)

      Regards Pankaj Joshi

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #3

      Why are you doing this? An MDI form contains a list of child forms. Use that to evaluate whether or not the form is present. A simplistic example would be:

      bool found = false;
      foreach (Form f in this.MdiChildren)
      {
          if (f.Text == "RibbonForm1")
          {
              found = true;
              f.TopMost = true;
          }
      }
      if (!found)
      {
          RibbonForm1 frm = new RibbonForm1();
          frm.MdiParent = this;
          frm.Show();
      }
      

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      P D S 3 Replies Last reply
      0
      • P Pete OHanlon

        Why are you doing this? An MDI form contains a list of child forms. Use that to evaluate whether or not the form is present. A simplistic example would be:

        bool found = false;
        foreach (Form f in this.MdiChildren)
        {
            if (f.Text == "RibbonForm1")
            {
                found = true;
                f.TopMost = true;
            }
        }
        if (!found)
        {
            RibbonForm1 frm = new RibbonForm1();
            frm.MdiParent = this;
            frm.Show();
        }
        

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        P Offline
        P Offline
        Pankaj Joshi
        wrote on last edited by
        #4

        Good solution ;) But this is OK when I am having more than 100 forms in my application. this will loop for each time 100 times..? Performance issue...:rolleyes:

        Regards Pankaj Joshi

        B P 2 Replies Last reply
        0
        • P Pete OHanlon

          Why are you doing this? An MDI form contains a list of child forms. Use that to evaluate whether or not the form is present. A simplistic example would be:

          bool found = false;
          foreach (Form f in this.MdiChildren)
          {
              if (f.Text == "RibbonForm1")
              {
                  found = true;
                  f.TopMost = true;
              }
          }
          if (!found)
          {
              RibbonForm1 frm = new RibbonForm1();
              frm.MdiParent = this;
              frm.Show();
          }
          

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          D Offline
          D Offline
          Deques
          wrote on last edited by
          #5

          (f.text == "RibbonForm1") what is it? could tell me that? and do i have to set the parent to mdicontainer? if i do that the background colors turn to gray. any way to change to the default color?

          P 1 Reply Last reply
          0
          • P Pankaj Joshi

            Good solution ;) But this is OK when I am having more than 100 forms in my application. this will loop for each time 100 times..? Performance issue...:rolleyes:

            Regards Pankaj Joshi

            B Offline
            B Offline
            Bekjong
            wrote on last edited by
            #6

            I'd just create a central form pool somewhere and use id's to fetch the form I want. Example:

            public class FormpoolClass
            {
            private Dictionary< string, Form > formPool = new Dictionary< string, form=>();

                public class CustomForm : Form
                {
            
                }
            
                public formType GetForm<formType>(string form\_id) where formType : Form
                {
                    if (this.formPool.ContainsKey(form\_id))             // check out if we have a reference to the form
                    {
                        if (formPool\[form\_id\] != null && !formPool\[form\_id\].IsDisposed)
                            return formPool\[form\_id\] as formType;            // if so, return it
                        else formPool.Remove(form\_id);
                    }
            
                    formType ret = null;
                    try
                    {
                        ret = Activator.CreateInstance<formType>();     // not there, so create it
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
            
                    Debug.Assert(ret != null);
                    return ret;
                }
            }
            
            public class Test
            {
                public void dotest()
                {
                    FormpoolClass fpc = new FormpoolClass();
                    CustomForm f = fpc.GetForm<customform&rt;("myform\_id");
                }
            }
            

            Standards are great! Everybody should have one!

            P 1 Reply Last reply
            0
            • D Deques

              (f.text == "RibbonForm1") what is it? could tell me that? and do i have to set the parent to mdicontainer? if i do that the background colors turn to gray. any way to change to the default color?

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #7

              This is just a simple way of telling who the child form is. I wouldn't rely on this, as it was only a quick sample, and the text is the thing that appears in the titlebar. You would create this form from the parent using.

              MyForm form = new MyForm();
              form.Parent = this;
              form.Show();
              

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              1 Reply Last reply
              0
              • B Bekjong

                I'd just create a central form pool somewhere and use id's to fetch the form I want. Example:

                public class FormpoolClass
                {
                private Dictionary< string, Form > formPool = new Dictionary< string, form=>();

                    public class CustomForm : Form
                    {
                
                    }
                
                    public formType GetForm<formType>(string form\_id) where formType : Form
                    {
                        if (this.formPool.ContainsKey(form\_id))             // check out if we have a reference to the form
                        {
                            if (formPool\[form\_id\] != null && !formPool\[form\_id\].IsDisposed)
                                return formPool\[form\_id\] as formType;            // if so, return it
                            else formPool.Remove(form\_id);
                        }
                
                        formType ret = null;
                        try
                        {
                            ret = Activator.CreateInstance<formType>();     // not there, so create it
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.ToString());
                        }
                
                        Debug.Assert(ret != null);
                        return ret;
                    }
                }
                
                public class Test
                {
                    public void dotest()
                    {
                        FormpoolClass fpc = new FormpoolClass();
                        CustomForm f = fpc.GetForm<customform&rt;("myform\_id");
                    }
                }
                

                Standards are great! Everybody should have one!

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #8

                That's exactly how I'd do it as well. Have yourself a 5 sir.

                Deja View - the feeling that you've seen this post before.

                My blog | My articles

                1 Reply Last reply
                0
                • P Pankaj Joshi

                  Good solution ;) But this is OK when I am having more than 100 forms in my application. this will loop for each time 100 times..? Performance issue...:rolleyes:

                  Regards Pankaj Joshi

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #9

                  And you are relying on adding a new condition and test everytime you add a new form. Hmm. Plus, how many times do you have 100 forms in an app? More importantly, as Bekjong states, I wouldn't rely on a loop test in my code. Instead, I'd use a generic dictionary to manage this. Then your code becomes a lot simpler and a lot faster.

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles

                  1 Reply Last reply
                  0
                  • D Deques

                    I have tried using the mdi method, but found out that it wasnt what I wanted. It opens a form within the parent form, which isnt the way I want What I want to do is to open a form from a parent form. If I use form.Show(); I can go back to the parent form and open the same form again. That I want not possible. If I use form.ShowDialog(); I cant go back to the parent form, which is not what I want either. What I want to do is to be able to go back to the parent form and open other forms, but not open the same form twice. How do I do that?

                    D Offline
                    D Offline
                    dan sh
                    wrote on last edited by
                    #10

                    You can do this using FormCollection class. Its object will have the list of all the forms opened by your application. FormCollection o = Application.OpenForms; I guess this might help.

                    Chaos, panic and disorder - my work here is done.

                    1 Reply Last reply
                    0
                    • P Pete OHanlon

                      Why are you doing this? An MDI form contains a list of child forms. Use that to evaluate whether or not the form is present. A simplistic example would be:

                      bool found = false;
                      foreach (Form f in this.MdiChildren)
                      {
                          if (f.Text == "RibbonForm1")
                          {
                              found = true;
                              f.TopMost = true;
                          }
                      }
                      if (!found)
                      {
                          RibbonForm1 frm = new RibbonForm1();
                          frm.MdiParent = this;
                          frm.Show();
                      }
                      

                      Deja View - the feeling that you've seen this post before.

                      My blog | My articles

                      S Offline
                      S Offline
                      Shpendh
                      wrote on last edited by
                      #11

                      its nice but i prefer to use if(f.Name == "RibbonForm1") and not if(f.Text == "RibbonForm1") because he can change the name dynamically respect.

                      spaps

                      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