Home Forums WinForms controls Other WinForms controls MdiList support in SmartMenuBar Reply To: MdiList support in SmartMenuBar

User (Old forums)
Member
Post count: 23064

Below is a first cut of a menu item that basically replaces itself with the list of mdi children (or is invisible if no children). It takes a bit of a brut force approach and I think it could be optimized quite a bit. It does seem to work they way I want it to, but I am not completely happy with how it does the job though.

Please let me know what you think or if you see any problems.

<u>Usage</u>
<pre>
Forms.Controls.WindowMdiListMenuItem wmmi = new WindowsAuditor.Forms.Controls.WindowMdiListMenuItem();
wmmi.MainForm = this;
mmiWindow.Items.Add(wmmi);
this.MdiChildActivate += new EventHandler(wmmi.MainForm_MdiChildActivate);
</pre>

<u>The Code</u>
<pre>
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
//using System.Windows.Forms;

using Xceed.SmartUI.Controls.MenuBar;

namespace Zabeda.Windows.Forms.Controls
{
/// <summary>
/// This class renders a list of MdiChild MenuItems. The method it uses to render the list
/// needs to be optimized, but it works in its current state.
/// </summary>
public class WindowMdiListMenuItem : Xceed.SmartUI.Controls.MenuBar.MenuItem
{
/// <summary>
/// A SeparatorMenuItem that can be reused
/// </summary>
private SeparatorMenuItem m_smi = new SeparatorMenuItem();

/// <summary>
/// for maintaining order
/// </summary>
private ArrayList m_mdiItems = new ArrayList();
/// <summary>
/// for maintaining order
/// </summary>
private ArrayList m_menuItems = new ArrayList();
/// <summary>
/// to find the menuitem by form – used when a form closes
/// </summary>
private Hashtable m_menuItemsHash = new Hashtable();

/// <summary>
/// This is in a hack to get around the order of events problem. <br/>
/// When an MDI child is closed, the MdiChildActivate event doesn’t tell you
/// what is happening (is child activating, openning or closing). So, when
/// a form is first activated we monitor the Closed event for the form. When
/// the form is Closed, we get an event notification, but the MdiActivate event
/// occurs again. The problem is that the closed form is still included in the
/// list of MdiChildren, even though it has been closed. This list is used
/// to hold a ref to a closed form, so that when the MdiChildActivate event occurs
/// we can check to see whether a form should really be included in the MdiChildItems
/// displayed in the menu.
/// </summary>
private ArrayList m_closedForms = new ArrayList();

// the form that owns the menu
private System.Windows.Forms.Form m_form;

private System.ComponentModel.IContainer components = null;

public WindowMdiListMenuItem()
{
// This call is required by the Windows Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitializeComponent call
this.Text = “WindowMdiList”;
base.Visible = false;
}

/// <summary>
/// The Form that owns the SmartMenuBar
/// </summary>
public System.Windows.Forms.Form MainForm
{
get { return m_form; }
set { m_form = value; }
}

/// <summary>
/// Will always return false
/// </summary>
public new bool Visible
{
get { return base.Visible; }
set { }
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Called when an MdiChild is activated, openned or closed in the MainForm
/// </summary>
/// <param name=”o”></param>
/// <param name=”e”></param>
public void MainForm_MdiChildActivate(object o, EventArgs e)
{

RemoveMdiChildrenMenuItems();
m_mdiItems.Clear();
m_menuItems.Clear();
m_menuItemsHash.Clear();
BuildMdiList();
RenderMdiList();
}

/// <summary>
/// Takes care of populating the list of MdiChild MenuItems that should
/// be rendered
/// </summary>
private void BuildMdiList()
{
foreach (System.Windows.Forms.Form mdiChild in MainForm.MdiChildren)
{
// see note above explaining the purpose of the m_closedForm list
if (m_closedForms.Contains(mdiChild))
{
// form won’t included in MdiList so remove form and let it be GC’d
m_closedForms.Remove(mdiChild);
}
else if (!m_mdiItems.Contains(mdiChild))
m_mdiItems.Add(mdiChild);
}
}

/// <summary>
/// Takes care of actually adding the MdiChild MenuItems to the MainMenuItem
/// </summary>
private void RenderMdiList()
{
if (m_mdiItems.Count > 0)
{
MainMenuItem mmi = (MainMenuItem) this.ParentItem;
m_menuItems.Add(m_smi); // store in list so it can be removed later
mmi.Items.Add(m_smi); // add to menu to separate mdi list items

int pos = 1;

foreach (Object o in m_mdiItems)
{
System.Windows.Forms.Form childForm = (System.Windows.Forms.Form) o;
childForm.Closed += new EventHandler(mdiChildForm_Closing);

MenuItem mi = new MenuItem(“&” + pos++ + ” ” + childForm.Text);
mi.Tag = childForm;
mi.Click += new Xceed.SmartUI.SmartItemClickEventHandler(mdiMenuItem_Click);

// add to main menu item
mmi.Items.Add( mi );
// save so we can clear later
m_menuItems.Add(mi);
// save so when form closes we can remove menuite

Imported from legacy forums. Posted by flahan (had 4256 views)