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. MDI children instances, how to code

MDI children instances, how to code

Scheduled Pinned Locked Moved C#
csstoolshelptutorialquestion
5 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.
  • I Offline
    I Offline
    ianhunt01
    wrote on last edited by
    #1

    Hi, I have trouble trying to find out which instance of a MDI child is active and how to adress it's properties and methods. I have got a MDI form with child frmList that shows all the available info in a particular case, then clicking on one of the nodes should pop up an frmShow that displays the info in a grid on a background image. Adding to this is a from frmProperties with certain tools that should depict what the user can do on the frmShow window (Be it zooming in, adding lines etc). The problem is that there must allways just be one instance of frmList and frmProperties but multiple instances of frmShow. I think I have it sorted with: (make suggestions please !)

            bool isOpen = false;
           // loop through list and find if child is open
           // 
            for (int x = 0; x < this.MdiChildren.Length; x++)
            {
                if (this.MdiChildren\[x\].Name == "frmListDef")
                {
                    isOpen = true;
                }
            }
           // if it does not exist open it
            if (!isOpen)
            {
                Form frmList = new frmListDef();
                frmList.MdiParent = this;
                frmList.Show();
            }
    

    Now I am trying to find out how to adress a method or property on the specific instance of a child ? thanks Ian PS: any nice articles or refs to MDI programming would be nice

    A D 2 Replies Last reply
    0
    • I ianhunt01

      Hi, I have trouble trying to find out which instance of a MDI child is active and how to adress it's properties and methods. I have got a MDI form with child frmList that shows all the available info in a particular case, then clicking on one of the nodes should pop up an frmShow that displays the info in a grid on a background image. Adding to this is a from frmProperties with certain tools that should depict what the user can do on the frmShow window (Be it zooming in, adding lines etc). The problem is that there must allways just be one instance of frmList and frmProperties but multiple instances of frmShow. I think I have it sorted with: (make suggestions please !)

              bool isOpen = false;
             // loop through list and find if child is open
             // 
              for (int x = 0; x < this.MdiChildren.Length; x++)
              {
                  if (this.MdiChildren\[x\].Name == "frmListDef")
                  {
                      isOpen = true;
                  }
              }
             // if it does not exist open it
              if (!isOpen)
              {
                  Form frmList = new frmListDef();
                  frmList.MdiParent = this;
                  frmList.Show();
              }
      

      Now I am trying to find out how to adress a method or property on the specific instance of a child ? thanks Ian PS: any nice articles or refs to MDI programming would be nice

      A Offline
      A Offline
      AhsanS
      wrote on last edited by
      #2

      If you want only one instance of a form to be open at a time use Singelton pattern

      Ahsan Ullah Senior Software Engineer

      1 Reply Last reply
      0
      • I ianhunt01

        Hi, I have trouble trying to find out which instance of a MDI child is active and how to adress it's properties and methods. I have got a MDI form with child frmList that shows all the available info in a particular case, then clicking on one of the nodes should pop up an frmShow that displays the info in a grid on a background image. Adding to this is a from frmProperties with certain tools that should depict what the user can do on the frmShow window (Be it zooming in, adding lines etc). The problem is that there must allways just be one instance of frmList and frmProperties but multiple instances of frmShow. I think I have it sorted with: (make suggestions please !)

                bool isOpen = false;
               // loop through list and find if child is open
               // 
                for (int x = 0; x < this.MdiChildren.Length; x++)
                {
                    if (this.MdiChildren\[x\].Name == "frmListDef")
                    {
                        isOpen = true;
                    }
                }
               // if it does not exist open it
                if (!isOpen)
                {
                    Form frmList = new frmListDef();
                    frmList.MdiParent = this;
                    frmList.Show();
                }
        

        Now I am trying to find out how to adress a method or property on the specific instance of a child ? thanks Ian PS: any nice articles or refs to MDI programming would be nice

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        The problem with this approach is the MdiChildren property of the parent is an array of forms so you can only see the standard properties and methods of the Form class unless you cast each member of the array to the appropriate class. That can be done by the code below. Assuming you have a class ChildForm1 : Form with a method DoSomething and property SomeProperty.

        foreach (Form child in this.MdiChildren)
        {
        if (child is ChildForm1)
        {
        ChildForm1 theChild = (ChildForm1)child;
        MessageBox.Show(theChild.SomeProperty);
        theChild.DoSomething();
        }
        }

        This is going to get messy if you have many forms with many properties and methods and will difficult to maintain. You could use reflection to get all the info about the child forms and their methods/properties but that may be a little slow depending on just how much reflection you need to do, but is probably the best solution here. Have you thought about creating an interface that your all child forms can implement as well as deriving form Form? You could add all the properties and methods that will be available in the children to the interface and implement them on the appropriate forms. The ones that shouldn't implement a specific property or method could throw a NotImplementedException so you know it's not available.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Expect everything to be hard and then enjoy the things that come easy. (code-frog)

        I 1 Reply Last reply
        0
        • D DaveyM69

          The problem with this approach is the MdiChildren property of the parent is an array of forms so you can only see the standard properties and methods of the Form class unless you cast each member of the array to the appropriate class. That can be done by the code below. Assuming you have a class ChildForm1 : Form with a method DoSomething and property SomeProperty.

          foreach (Form child in this.MdiChildren)
          {
          if (child is ChildForm1)
          {
          ChildForm1 theChild = (ChildForm1)child;
          MessageBox.Show(theChild.SomeProperty);
          theChild.DoSomething();
          }
          }

          This is going to get messy if you have many forms with many properties and methods and will difficult to maintain. You could use reflection to get all the info about the child forms and their methods/properties but that may be a little slow depending on just how much reflection you need to do, but is probably the best solution here. Have you thought about creating an interface that your all child forms can implement as well as deriving form Form? You could add all the properties and methods that will be available in the children to the interface and implement them on the appropriate forms. The ones that shouldn't implement a specific property or method could throw a NotImplementedException so you know it's not available.

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Expect everything to be hard and then enjoy the things that come easy. (code-frog)

          I Offline
          I Offline
          ianhunt01
          wrote on last edited by
          #4

          Thanks Dave, It works !! Do you know of a good reference on more MDI topics ? A step by step guide to get abit more out of my MDI code ! cheers Ian

          D 1 Reply Last reply
          0
          • I ianhunt01

            Thanks Dave, It works !! Do you know of a good reference on more MDI topics ? A step by step guide to get abit more out of my MDI code ! cheers Ian

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

            Not really - MDI is much the same as any other winforms app. The only suggetions I would give are: 1. Get very familiar with events and delegates as inevitably you'll find the need to get your child forms communicating with their parent and siblings. 2. All your child forms are actually hosted in a container which is an instance of the MdiClient class. You may find sometimes it's useful to have access to that control. Creating this readonly property and member variable in your MdiParent will make this much easier.

                private MdiClient m\_MdiClient;
                public MdiClient MdiClient
                {
                    get
                    {
                        if (m\_MdiClient != null)
                            return m\_MdiClient;
                        foreach (Control control in Controls)
                        {
                            if (control is MdiClient)
                            {
                                m\_MdiClient = (MdiClient)control;
                                return m\_MdiClient;
                            }
                        }
                        throw new Exception("MdiClient control could not be found");
                    }
                }
            

            You then have access to all the containers properties, methods and events!

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Expect everything to be hard and then enjoy the things that come easy. (code-frog)

            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