• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 735
  • Last Modified:

Managing grandchild forms with Window menu

I see that you can create forms in forms as long as you aren't trying to nest MDI windows.  If you want to be able to have the ability to bring subforms to the front via a menu similar to the standard "Window" menu how would you do that?

The scenario is this:
Main form has a child form "Child" in it.
Child has "Grandchild 1" in it.
Child has "Grandchild 2" in it.
I would like a "Window" menu in Main that allows the user to select which grandchild form to bring to the front within Child.  This Window menu would need to be updated whenever a "grandchild" form opens or closes in Child.
0
larryh
Asked:
larryh
  • 8
  • 7
1 Solution
 
Bob LearnedCommented:
You will need to find a way to get a reference to the grand children forms from the main form.  Where are these forms created?
0
 
larryhAuthor Commented:
A grandchild form would be created by way of the app menu.  The grandchild form would put itself inside the child form (via childForm.Controls.Add).
0
 
Bob LearnedCommented:
Is the "app menu" on the main form?

Imagine, if you will, that I don't know anything about your application...
Imagine sitting where I sit, and dealing with vague problem descriptions...
Imagine how long this question will take to get answered...
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
larryhAuthor Commented:
Sorry.  I wasn't quite sure what you meant by "where was the form created".

The "app menu" is on the main MDI parent form.  On the menu is a "Window" menu item.  Normally that would show the list of MDI child forms.  I want a group of forms (the grandchildren) to only open within a specific child form.  By default the child form will show up in the Window menu but not the grandchild forms.  Since you can't nest MDI forms that is as good as the WIndow menu gets by itself.  Can this Window menuitem be built at run time as grandchild forms are created so that it would show the list of grandchild forms and then bring the selected grandchild form to the front?  
I hope this gives you better detail.
0
 
Bob LearnedCommented:


It sounds like you are looking for code to generate MenuItem instances at run-time, right?
0
 
larryhAuthor Commented:
I noticed the message duplication as well.  I noticed that this message has 3 zones.  I also noticed that it is related to another solution.

I just type text and click the "Submit" button in the "Post Comment/Solution" section at the bottom of the form.  I don't do anything else.  I am now going to click the "Submit" button once...
0
 
larryhAuthor Commented:
TheLearnedOne

Yes, to generate menuitems at runtime but perhaps more importantly when those menuitems are selected to bring the associated form to the front of the other grandchild forms (all the while keeping the grandchild forms within the child form).
--I'm sorry if this message gets duplicated.  I hope it doesn't but no one has discovered a solution yet.
0
 
Bob LearnedCommented:
That now sounds more like you are looking to associate a MenuItem with a form when the MenuItem is clicked.
0
 
larryhAuthor Commented:
Yes.  That is correct.  I want the menu to work like the standard "Window" menu in MS Windows applications but work on grandchild forms within a child form.  Since you cannot nest MDIParents I figured there might be a way to simulate it.
0
 
Bob LearnedCommented:
I am still trying to understand your words, and I am honestly having a difficult time.  Since I am a visual learner, I appreciate images over words.  What I think would help here is if you attach a .png screen shot of what your UI looks like, so that I can gain a better understanding of your requirements.
0
 
larryhAuthor Commented:
OK.  The snapshot is attached.  I would like to add menuItems to the Window menu when each new grandchild form is opened.  That menuItem when clicked would bring that particular grandchild form to the front of the other grandchild forms.  Does this help explain my situation?
GrandChildren.PNG
0
 
Bob LearnedCommented:
I believe that you might be looking for something like the attached prototype.

1) Created MainForm, ChildForm, and GrandChildForm instances.

2) Came up with this sample code:


using System;
using System.Windows.Forms;
 
 
namespace WindowsApplication1
{
    public partial class MainForm : System.Windows.Forms.Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
 
        private ChildForm _Child = new ChildForm();
        private GrandChildForm _GrandChild1 = new GrandChildForm();
        private GrandChildForm _GrandChild2 = new GrandChildForm();
 
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.IsMdiContainer = true;
 
            _Child.Text = "Child Form";
            _Child.MdiParent = this;
            _Child.Dock = System.Windows.Forms.DockStyle.Fill;
            _Child.Show();
 
            _GrandChild1.TopLevel = false;
            _GrandChild1.Parent = _Child;
            _GrandChild1.Height = (int)(_Child.Height * .5);
            _GrandChild1.Width = (int)(_Child.Width * .5);
            _GrandChild1.Show();
 
            _GrandChild2.TopLevel = false;
            _GrandChild2.Parent = _Child;
            _GrandChild2.Left = _GrandChild1.Left + _GrandChild1.Width / 2;
            _GrandChild2.Top = _GrandChild1.Top + _GrandChild1.Height / 2;
            _GrandChild2.Height = (int)(_Child.Height * .5);
            _GrandChild2.Width = (int)(_Child.Width * .5);
            _GrandChild2.Show();
        }
 
        private void MainForm_Shown(object sender, EventArgs e)
        {
            this.WindowMenu.DropDownItems.Add(new ToolStripSeparator());
 
            AddWindowMenuItem("Grand Child 1", _GrandChild1, true);
            AddWindowMenuItem("Grand Child 2", _GrandChild2, false);
        }
 
        private void AddWindowMenuItem(string text, GrandChildForm form, bool isChecked)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(text);
            item.Tag = form;
            item.Click += new EventHandler(MenuItem_Click);
            item.Checked = isChecked;
            this.WindowMenu.DropDownItems.Add(item);
        }
 
        private void MenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            GrandChildForm form = (GrandChildForm)item.Tag;
            form.BringToFront();
 
            // Uncheck all the menu items, to check the current one.
            foreach (ToolStripItem scan in this.WindowMenu.DropDownItems)
            {
                if (scan.Tag != null)
                {
                    (scan as ToolStripMenuItem).Checked = false;
                }
            }
 
            item.Checked = true;
        }
 
    }
}

Open in new window

0
 
larryhAuthor Commented:
Thanks for this start.  When I run this I see the two grandchild forms and I can switch by clicking on their title bars or via the Window menu.  One thing I noticed is that I can't bring a grandchild form to the front by simply clicking in the form.  I would not have expected that.  You have to click somewhere on the form's boundary (title bar or edges).  If you have a text box on the grandchild forms you can click in one that's in the back and focus shifts there without bringing that form to the front.

Am I perhaps asking for too much?
0
 
Bob LearnedCommented:
If you have grand-child form, you can't bring it to the front of all forms.  It would still be owned by the form one level up, so it would be constricted to that region.  How are you creating and showing the grandchildren?
0
 
larryhAuthor Commented:
I understand that the grandchildren forms are bound by their parent form.  That is the whole reason for this exercise.  I assumed that if I clicked anywhere within a grandchild form it would bring that grandchild form to the front of all other grandchild forms that belonged to the common parent.  This only seems to work if I click on the form borders (including the title bar at the top of the form).  This just may be a limitation of the approach.  I'll have to play around with it to see if the form.Click event can be used to bring it to the front via code instead of natively.

I will mark your code sample as the accepted solution.  You've been very patient with me.  Thank you for your time and effort.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now