Home » C# ProgrammingRSS

How do you merge a ContextMenuStrip with a MenuStrip?

I have a MenuStrip with 3 items on it: File, Edit and Help.

I'd like to merge a ContextMenuStrip I've created with the Edit sub menu.  I've been scratching my head and trying without success now for over an hour. 

I've been attempting to use ToolStripManager.Merge() am I on the right track?

Please help!

 

6 Answers Found

 

Answer 1

merge menu will create submenu item, for Example

if Main menu contains - File,Edit and Help, and Contextmenu - Open

if you merge context menu to Main menu then - it will like

Main Menu  |  Open.

------------------------

File              |

Edit             |

Help            |

this format, but if your intension is to insert edit menu to the context menu,

you cant merge to contextmenu, it can be done with the context menuitem, here you can merge the open menuitem to the edit menuitem

context menu is menu, so we can merge it with menu not menuitem.

 if you realy want to assign Main menu's menuitem to the Context menu, you can use this method          

ContextMenu.MenuItems.Add

-thanks

 

Answer 2

Thanks for your reply.  Sorry but I'm still slightly confused.

I'd like to merge the contents of my ContextMenuStrip, e.g.

Item1

Item2

Item3

into the Edit menu which is contained in the MenuStrip (has menus File, Edit, and Help).

So under the Edit menu I'd like to have Item1, Item2, and Item3.

Could someone please provide a simple code sample for me?  Thanks.

I've tried:

contextMenuStrip.AllowMerge = true;

menuStrip.AllowMerge = true;

ToolStripManager.Merge(contextMenuStrip, menuStrip); // returns false :(

 

Answer 3

Try this one,

mnuItemEdit.MergeMenu( contextMenuStrip );

now  Edit menu have following sub menu's

item1

item2

item3.

 Is that you want.

 -thanks

 

Answer 4

That won't work.  Here's my code:

MenuStrip menuStrip; // Main menu

ContextMenuStrip contextMenuStrip; // Context menu

....

....

ToolStripMenuItem editMenu = (ToolStripMenuItem)menuStrip.Items[1]; // Edit submenu

Can someone please tell me what I need to add.  Thanks!

 

Answer 5

Okay I worked it out myself.  I feared that I would retire before someone could help me :)

editMenu.DropDown = contextMenuStrip;

 

Answer 6

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ToolStripManagerTest
{
  publicpartialclass Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    privatevoid textBox1_MouseDown(object sender, MouseEventArgs e)
    {
      if (e.Button == MouseButtons.Right)
        ToolStripManager.Merge(this.contextMenuStrip1, this.contextMenuStrip2);
    }

    privatevoid Form1_MouseDown(object sender, MouseEventArgs e)
    {
      if (e.Button == MouseButtons.Right)
        ToolStripManager.RevertMerge(this.contextMenuStrip2, this.contextMenuStrip1);
    }

    ///<summary>/// Required designer variable.///</summary>private System.ComponentModel.IContainer components = null;

    ///<summary>/// Clean up any resources being used.///</summary>///<param name="disposing">true if managed resources should be disposed; otherwise, false.</param>protectedoverridevoid Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    ///<summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor.///</summary>privatevoid InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
      this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
      this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.contextMenuStrip2 = new System.Windows.Forms.ContextMenuStrip(this.components);
      this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem();
      this.toolStripMenuItem4 = new System.Windows.Forms.ToolStripMenuItem();
      this.contextMenuStrip1.SuspendLayout();
      this.contextMenuStrip2.SuspendLayout();
      this.SuspendLayout();
      // // contextMenuStrip1// this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
      this.toolStripMenuItem1,
      this.toolStripMenuItem2});
      this.contextMenuStrip1.Name = "contextMenuStrip1";
      this.contextMenuStrip1.Size = new System.Drawing.Size(153, 70);
      // // toolStripMenuItem1// this.toolStripMenuItem1.MergeAction = System.Windows.Forms.MergeAction.Insert;
      this.toolStripMenuItem1.MergeIndex = 0;
      this.toolStripMenuItem1.Name = "toolStripMenuItem1";
      this.toolStripMenuItem1.Size = new System.Drawing.Size(152, 22);
      this.toolStripMenuItem1.Text = "A1";
      // // toolStripMenuItem2// this.toolStripMenuItem2.MergeAction = System.Windows.Forms.MergeAction.Insert;
      this.toolStripMenuItem2.MergeIndex = 1;
      this.toolStripMenuItem2.Name = "toolStripMenuItem2";
      this.toolStripMenuItem2.Size = new System.Drawing.Size(152, 22);
      this.toolStripMenuItem2.Text = "A2";
      // // textBox1// this.textBox1.ContextMenuStrip = this.contextMenuStrip2;
      this.textBox1.Location = new System.Drawing.Point(45, 73);
      this.textBox1.Multiline = true;
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(210, 148);
      this.textBox1.TabIndex = 1;
      this.textBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDown);
      // // contextMenuStrip2// this.contextMenuStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
      this.toolStripMenuItem3,
      this.toolStripMenuItem4});
      this.contextMenuStrip2.Name = "contextMenuStrip2";
      this.contextMenuStrip2.Size = new System.Drawing.Size(99, 48);
      // // toolStripMenuItem3// this.toolStripMenuItem3.Name = "toolStripMenuItem3";
      this.toolStripMenuItem3.Size = new System.Drawing.Size(98, 22);
      this.toolStripMenuItem3.Text = "A3";
      // // toolStripMenuItem4// this.toolStripMenuItem4.Name = "toolStripMenuItem4";
      this.toolStripMenuItem4.Size = new System.Drawing.Size(98, 22);
      this.toolStripMenuItem4.Text = "A4";
      // // Form1// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(292, 266);
      this.ContextMenuStrip = this.contextMenuStrip1;
      this.Controls.Add(this.textBox1);
      this.Name = "Form1";
      this.Text = "Form1";
      this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
      this.contextMenuStrip1.ResumeLayout(false);
      this.contextMenuStrip2.ResumeLayout(false);
      this.ResumeLayout(false);
      this.PerformLayout();

    }

    #endregionprivate System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
    private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
    private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem2;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.ContextMenuStrip contextMenuStrip2;
    private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem3;
    private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem4;
  }
}


 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter