Sharing MenuStrip between Forms

1k views Asked by At

I have a question a litle bit connected with this one: How to create navigation inside program Form - noob alert again.

I'm trying to create a navigation inside c# program like a dynamic dropdown menu on website. I'm using diferent C# Forms like each and every one one was a different webpage with content.

Is is posible to share menustrip with navigation between Forms? (simplest web similarity: include('menu.php') - put everything into one file I inlcude on every webpage )

Otherwise it would require to copy menustrip to each new Form (then MenuStrip changes, and then what? Copy them again ? :) )

2

There are 2 answers

0
baron_bartek On BEST ANSWER

You can do it via joining tabs with Srip Meni like this:

//make tabs invisible (so you wont see them changing)
private void HideAllTabsOnTabControl(TabControl theTabControl)
{
    theTabControl.Appearance = TabAppearance.FlatButtons;
    theTabControl.ItemSize = new Size(0, 1);
    theTabControl.SizeMode = TabSizeMode.Fixed;
}

//hide all tabs
private void hide_all_tabs() {
    Tab_content.TabPages.Remove(tabPage1);
    Tab_content.TabPages.Remove(tabPage2);
    Tab_content.TabPages.Remove(tabPage3);
    Tab_content.TabPages.Remove(tabPage4);
}
private void materiaƂyQCToolStripMenuItem_Click(object sender, EventArgs e)
{
    hide_all_tabs(); //hide everything
    Tab_content.TabPages.Add(tabPage1); //schow just first tab
}
private void analizatoryQCToolStripMenuItem_Click(object sender, EventArgs e)
{
    hide_all_tabs();
    Tab_content.TabPages.Add(tabPage2);
}
private void QCzDzisiajToolStripMenuItem_Click(object sender, EventArgs e)
{
    hide_all_tabs();
    Tab_content.TabPages.Add(tabPage3);
}
private void standlabToolStripMenuItem_Click(object sender, EventArgs e)
{
    hide_all_tabs();
    Tab_content.TabPages.Add(tabPage4);
}
0
Joaquim Varandas On

For instance, if you want to share items between a ContextMenu and the main Form MenuStrip

No items defined

MenuStrip without items

enter image description here

ContextMenu items

enter image description here

ContextMenu with copied items from ContextMenu

On main Form you can do:

ToolStripMenuItem toolStripMoved;
ToolStripItem[] itemsArrayMoved;

private void Edit_DropDownOpening(object sender, EventArgs e)
{
    var doc = DockPanel.GetDocuments().FirstOrDefault(t => t.Visible) as DockDocument;
    if (doc != null)
    {
        toolStripMoved = doc.ContextMenuStrip.Items["Edit"] as ToolStripMenuItem;
        if (toolStripMoved != null)
        {
            var menu = (sender as ToolStripMenuItem);
            itemsArrayMoved = Helper.GetAllChildren(toolStripMoved);
            foreach (ToolStripItem item in itemsArrayMoved)
            {
                if (item.Name.Equals("Edit") == false)
                    menu.DropDownItems.Add(item);
            }
        }
    }
}

private void Edit_DropDownClosed(object sender, EventArgs e)
{
    if (toolStripMoved != null)
    {
        foreach (ToolStripItem item in itemsArrayMoved)
        {
            if (item.Name.Equals("Edit") == false)
                toolStripMoved.DropDownItems.Add(item);
        }
    }
}

static private ToolStripItem[] GetAllChildren(ToolStripItem item)
{
    List<ToolStripItem> Items = new List<ToolStripItem>() { item };
    if (item is ToolStripMenuItem)
        foreach (ToolStripItem i in ((ToolStripMenuItem)item).DropDownItems)
            Items.AddRange(GetAllChildren(i));
    else if (item is ToolStripSplitButton)
        foreach (ToolStripItem i in ((ToolStripSplitButton)item).DropDownItems)
            Items.AddRange(GetAllChildren(i));
    else if (item is ToolStripDropDownButton)
        foreach (ToolStripItem i in ((ToolStripDropDownButton)item).DropDownItems)
            Items.AddRange(GetAllChildren(i));
    return Items.ToArray();
}