我将将自定义contextMenuStrip添加到已经默认contextMenuStrip的控件中。
代码片段:
DBDisplay imageControl = new DBDisplay(); // This is third-party object
imageControl.ContextMenuStrip = this.contextMenuStripTableLayout;但是,默认的contextMenu已更改为新的contextMenu。我想使用默认+新的contextMenu。有什么建议或帮助吗?
谢谢格兰特。我还有一个问题。
更新编辑
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++;
}
}我还有一个问题。如果我使用上面的代码,那么imageControl contextMenuStrip会重复10次。我该如何解决这个问题?
更新基本上,m_pImage列表将添加到tableLayoutPanel1控件中。我将使用每一列和每一行内容。接近是正确的吗?
Update2 Ok,这是详细的源代码。DBDisplay是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();
}
}
}下面是CogDisplay类的对象描述。http://www.hyperbook.com/cognex/vpro/ja/Cognex.VisionPro.Display.CogDisplay.html
发布于 2013-06-17 01:41:32
对于ContextMenuStrip,请尝试使用ToolStripManager.Merge。我不知道这在你的情况下是否有效,但通常它会将第一个菜单中的所有项目合并到第二个菜单中。我以前在两个ContextMenuStrip实例中使用过这种方法,但是我不知道DBDisplay是什么,也不知道它是如何内部设计的。
if (ToolStripManager.Merge(imageControl.ContextMenuStrip, newContextMenuStrip))
imageControl.ContextMenuStrip = newContextMenuStrip;首先您说的是ContextMenu,然后您的控件似乎实际上实现了一个ContextMenuStrip。以防万一,对于ContextMenu,您可以尝试使用Menu.MergeMenu
imageControl.ContextMenu.MergeMenu(newContextMenu);编辑:(在Changju更新了原问题后)
您正在将所有10个imageControl菜单合并到contextMenuStripTableLayout中,因此可以看到菜单项重复了10次。
我认为您只需使用循环来添加它们:
foreach (ToolStripItem tsItem in contextMenuStripTableLayout.Items)
imageControl.ContextMenuStrip.Items.Add(tsItem);https://stackoverflow.com/questions/17138947
复制相似问题