Reputation: 471
I'm going to add customizing contextMenuStrip to a control which has already default contextMenuStrip .
Code snippet :
DBDisplay imageControl = new DBDisplay(); // This is third-party object
imageControl.ContextMenuStrip = this.contextMenuStripTableLayout;
However, default contextMenu has been changed new contextMenu. I'd like to use default + new contextMenu. Is there any tip or help ?
Thanks Grant. I have one more question.
updated edit
List<DBDisplay> m_pImage = new List<DBDisplay>();
for (int i = 0; i < 10; i++)
{
DBDisplay imageControl = new DBDisplay();
imageControl.Location = new System.Drawing.Point(0, 0);
imageControl.Dock = System.Windows.Forms.DockStyle.Fill;
imageControl.Size = new Size(columnWidth, rowHeight);
foreach (ToolStripItem tsItem in contextMenuStripTableLayout.Items)
imageControl.ContextMenuStrip.Items.Add(tsItem);
m_pImage.Add(imageControl);
}
int index = 0;
for (int i = 0; i < columnCount; i++)
{
//First add a column
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, columnWidth));
for (int j = 0; j < rowCount; j++)
{
if (i == 0)
{
//defining the size of cell
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));
}
tableLayoutPanel1.Controls.Add(m_pImage[index], i, j);
index++;
}
}
I have one more question. If I use above code, then imageControl contextMenuStrip has 10 times repeat. How can I solve this problem ?
Update Basically, m_pImage list will be added in tableLayoutPanel1 control. I'm going to use each column and row contextmenustrip. Is it correct to approach or not ?
Update2 Ok, here is detail source code. DBDisplay is DBCogDisplay.
using System.ComponentModel;
using System.Windows.Forms;
using Cognex.VisionPro.Display;
namespace DBSubClass
{
public partial class DBCogDisplay : Cognex.VisionPro.Display.CogDisplay
{
public DBCogDisplay()
{
InitializeComponent();
SetLayoutSettings();
SetStyleSettings();
}
public DBCogDisplay(IContainer container)
{
container.Add(this);
InitializeComponent();
SetLayoutSettings();
SetStyleSettings();
}
private void SetLayoutSettings()
{
base.AllowDrop = true;
base.Dock = System.Windows.Forms.DockStyle.Fill;
base.Location = new System.Drawing.Point(0, 0);
base.MouseWheelMode = CogDisplayMouseWheelModeConstants.Zoom1;
base.ContextMenuStrip.AllowMerge = true;
}
private void SetStyleSettings()
{
base.DoubleBuffered = true;
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
UpdateStyles();
}
}
}
Here is Object description of CogDisplay class. http://www.hyperbook.com/cognex/vpro/ja/Cognex.VisionPro.Display.CogDisplay.html
Upvotes: 0
Views: 622
Reputation: 66449
For a ContextMenuStrip
, try using ToolStripManager.Merge. I have no idea if this will work in your case, but normally it takes all the items in the first menu and merges them into the second menu. I've used this on two instances of ContextMenuStrip
before, but I don't know what a DBDisplay
is or how it's designed internally.
if (ToolStripManager.Merge(imageControl.ContextMenuStrip, newContextMenuStrip))
imageControl.ContextMenuStrip = newContextMenuStrip;
First you said ContextMenu
, but then your control seems to actually implement a ContextMenuStrip
. Just in case, for a ContextMenu
you could try using Menu.MergeMenu:
imageControl.ContextMenu.MergeMenu(newContextMenu);
Edit: (after Changju updated the original question)
You're merging all 10 imageControl
menus into contextMenuStripTableLayout
, so you're seeing the menu items repeated 10 times.
I think you'll just have to add them using a loop instead:
foreach (ToolStripItem tsItem in contextMenuStripTableLayout.Items)
imageControl.ContextMenuStrip.Items.Add(tsItem);
Upvotes: 1