Click here to Skip to main content
15,885,309 members
Home / Discussions / C#
   

C#

 
Questionproxy problem Pin
mstfync14-Jul-10 21:02
mstfync14-Jul-10 21:02 
AnswerRe: proxy problem Pin
Pete O'Hanlon14-Jul-10 21:49
mvePete O'Hanlon14-Jul-10 21:49 
QuestionAccessing mdiParent form menu from mdiChild form via code [solved] Pin
AussieLew14-Jul-10 19:36
AussieLew14-Jul-10 19:36 
AnswerRe: Accessing mdiParent form menu from mdiChild form via code Pin
Peace ON14-Jul-10 19:48
Peace ON14-Jul-10 19:48 
GeneralRe: Accessing mdiParent form menu from mdiChild form via code Pin
AussieLew14-Jul-10 22:09
AussieLew14-Jul-10 22:09 
AnswerRe: Accessing mdiParent form menu from mdiChild form via code Pin
Peace ON14-Jul-10 22:49
Peace ON14-Jul-10 22:49 
GeneralRe: Accessing mdiParent form menu from mdiChild form via code Pin
Dave Kreskowiak15-Jul-10 2:02
mveDave Kreskowiak15-Jul-10 2:02 
AnswerRe: Accessing mdiParent form menu from mdiChild form via code [modified] Pin
DaveyM6914-Jul-10 23:54
professionalDaveyM6914-Jul-10 23:54 
I would do it in a slightly long winded way, but it's far more flexible/extendable in future.

All child forms that need this functionality should raise an event to signal to the parent that it wants to update it's menu. The children should never have access to the menu directly! The best way to do this is to have a enumeration of menu items that you can add to as required.
C#
// UpdatableMenuItem.cs

namespace YourMdiApp
{
    public enum UpdatableMenuItem
    {
        File,
        Exit,
        View,
    }
}

Now you can create a class to pass as an argument with the event that details the item and the change required.
C#
// UpdateMenuItemEventArgs.cs
using System;

namespace YourMdiApp
{
    public delegate void UpdateMenuItemEventHandler(object sender, UpdateMenuItemEventArgs e);

    public class UpdateMenuItemEventArgs : EventArgs
    {
        private bool itemChecked;
        private bool enabled;
        private UpdatableMenuItem item;
        private bool visible;

        public UpdateMenuItemEventArgs(UpdatableMenuItem item, bool itemChecked, bool enabled, bool visible)
        {
            this.item = item;
            this.itemChecked = itemChecked;
            this.enabled = enabled;
            this.visible = visible;
        }

        public bool Checked
        {
            get { return itemChecked; }
        }
        public bool Enabled
        {
            get { return enabled; }
        }
        public UpdatableMenuItem Item
        {
            get { return item; }
        }
        public bool Visible
        {
            get { return visible; }
        }
    }
}

Now you need a base class that all your child forms can derive from that has the event and methods to raise it
C#
// FormChildBase.cs
using System.Windows.Forms;

namespace YourMdiApp
{
    public class FormChildBase : Form
    {
        public event UpdateMenuItemEventHandler UpdateMenuItem;

        protected virtual void OnUpdateMenuItem(UpdateMenuItemEventArgs e)
        {
            UpdateMenuItemEventHandler eh = UpdateMenuItem;
            if (eh != null)
                eh(this, e);
        }
        public void PerformUpdateMenuItem(UpdatableMenuItem item, bool itemChecked, bool enabled, bool visible)
        {
            OnUpdateMenuItem(new UpdateMenuItemEventArgs(item, itemChecked, enabled, visible));
        }
    }
}

With this done, change your child forms to derive from FormChildBase instead of Form and call PerformUpdate when required.
C#
// FormChildA.cs
using System;

namespace YourMdiApp
{
    public partial class FormChildA : FormChildBase
    {
        public FormChildA()
        {
            InitializeComponent();
            /* Clicking this form will disable the File menu on the main form */
            Click += new EventHandler(FormChildA_Click);
        }

        void FormChildA_Click(object sender, EventArgs e)
        {
            PerformUpdateMenuItem(UpdatableMenuItem.File, false, false, true);
        }
    }
}

All that's left to do is subscribe to the child's UpdateMenuItem event from the parent and update the menu accordingly - for example...
C#
// FormMain.cs
/* Menu and items added in designer and IsMdiContainer property set to true */
using System.Windows.Forms;

namespace YourMdiApp
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            FormChildA formChildA = new FormChildA();
            formChildA.UpdateMenuItem += new UpdateMenuItemEventHandler(formChildA_UpdateMenuItem);
            formChildA.MdiParent = this;
            formChildA.Show();
        }

        void formChildA_UpdateMenuItem(object sender, UpdateMenuItemEventArgs e)
        {
            ToolStripMenuItem item = null;
            switch (e.Item)
            {
                case UpdatableMenuItem.File:
                    item = fileToolStripMenuItem;
                    break;
                // ...
            }
            if (item != null)
            {
                item.Checked = e.Checked;
                item.Enabled = e.Enabled;
                item.Visible = e.Visible;
            }
        }
    }
}

Edit: Better still, you could keep a dictionary that maps UpdatableMenuItems to the actual menu items so FormMain becomes...
C#
// FormMain.cs
/* Menu and items added in designer and IsMdiContainer property set to true */
using System.Collections.Generic;
using System.Windows.Forms;

namespace YourMdiApp
{
    public partial class FormMain : Form
    {
        private Dictionary<UpdatableMenuItem, ToolStripMenuItem> menuItemDictionary;

        public FormMain()
        {
            InitializeComponent();
            menuItemDictionary = new Dictionary<UpdatableMenuItem, ToolStripMenuItem>()
            {
                { UpdatableMenuItem.File, fileToolStripMenuItem }
            };
            FormChildA formChildA = new FormChildA();
            formChildA.UpdateMenuItem += new UpdateMenuItemEventHandler(formChildA_UpdateMenuItem);
            formChildA.MdiParent = this;
            formChildA.Show();
        }

        void formChildA_UpdateMenuItem(object sender, UpdateMenuItemEventArgs e)
        {
            ToolStripMenuItem item;
            if (menuItemDictionary.TryGetValue(e.Item, out item))
            {
                item.Checked = e.Checked;
                item.Enabled = e.Enabled;
                item.Visible = e.Visible;
            }
        }
    }
}

Dave

If this helped, please vote & accept answer!


Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

modified on Thursday, July 15, 2010 7:02 AM

GeneralRe: Accessing mdiParent form menu from mdiChild form via code Pin
AussieLew15-Jul-10 23:11
AussieLew15-Jul-10 23:11 
GeneralRe: Accessing mdiParent form menu from mdiChild form via code Pin
DaveyM6917-Jul-10 0:35
professionalDaveyM6917-Jul-10 0:35 
GeneralRe: Accessing mdiParent form menu from mdiChild form via code Pin
AussieLew27-Jul-10 13:08
AussieLew27-Jul-10 13:08 
GeneralRe: Accessing mdiParent form menu from mdiChild form via code Pin
DaveyM6930-Jul-10 10:47
professionalDaveyM6930-Jul-10 10:47 
GeneralRe: Accessing mdiParent form menu from mdiChild form via code Pin
AussieLew30-Jul-10 22:51
AussieLew30-Jul-10 22:51 
GeneralRe: Accessing mdiParent form menu from mdiChild form via code Pin
DaveyM6931-Jul-10 0:13
professionalDaveyM6931-Jul-10 0:13 
QuestionList.Sort() Stability? Reliability...? Pin
ely_bob14-Jul-10 14:41
professionalely_bob14-Jul-10 14:41 
AnswerRe: List.Sort() Stability? Reliability...? Pin
Luc Pattyn14-Jul-10 15:17
sitebuilderLuc Pattyn14-Jul-10 15:17 
GeneralRe: List.Sort() Stability? Reliability...? Pin
Ennis Ray Lynch, Jr.15-Jul-10 3:22
Ennis Ray Lynch, Jr.15-Jul-10 3:22 
GeneralRe: List.Sort() Stability? Reliability...? Pin
Richard Blythe15-Jul-10 6:37
Richard Blythe15-Jul-10 6:37 
GeneralRe: List.Sort() Stability? Reliability...? Pin
Richard Blythe15-Jul-10 6:39
Richard Blythe15-Jul-10 6:39 
AnswerRe: List.Sort() Stability? Reliability...? Pin
Luc Pattyn15-Jul-10 7:15
sitebuilderLuc Pattyn15-Jul-10 7:15 
AnswerRe: List.Sort() Stability? Reliability...? Pin
Pete O'Hanlon15-Jul-10 10:37
mvePete O'Hanlon15-Jul-10 10:37 
QuestionHow do I format Column when a Gridview loads? Pin
roman_s14-Jul-10 9:14
roman_s14-Jul-10 9:14 
AnswerRe: How do I format Column when a Gridview loads? Pin
Luc Pattyn14-Jul-10 9:35
sitebuilderLuc Pattyn14-Jul-10 9:35 
GeneralRe: How do I format Column when a Gridview loads? Pin
roman_s14-Jul-10 10:06
roman_s14-Jul-10 10:06 
GeneralRe: How do I format Column when a Gridview loads? Pin
Luc Pattyn14-Jul-10 10:18
sitebuilderLuc Pattyn14-Jul-10 10:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.