【问题标题】:How to add contextmenu in control which has original contextmenu?如何在具有原始上下文菜单的控件中添加上下文菜单?
【发布时间】:2013-06-17 00:13:54
【问题描述】:

我将添加自定义 contextMenuStrip 到一个已经默认 contextMenuStrip 的控件。

代码 sn-p:

   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 好的,这里是详细的源代码。 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

【问题讨论】:

    标签: c# winforms contextmenu


    【解决方案1】:

    对于ContextMenuStrip,请尝试使用ToolStripManager.Merge。我知道这是否适用于您的情况,但通常它会获取第一个菜单中的所有项目并将它们合并到第二个菜单中。我之前在ContextMenuStrip 的两个实例上使用过它,但我不知道DBDisplay 是什么,也不知道它是如何内部设计的。

    if (ToolStripManager.Merge(imageControl.ContextMenuStrip, newContextMenuStrip))
        imageControl.ContextMenuStrip = newContextMenuStrip;
    

    首先你说ContextMenu,但是你的控件似乎实际上实现了ContextMenuStrip。以防万一,对于ContextMenu,您可以尝试使用Menu.MergeMenu

    imageControl.ContextMenu.MergeMenu(newContextMenu);
    

    编辑:(长居更新原题后)

    您将所有 10 个 imageControl 菜单合并到 contextMenuStripTableLayout 中,因此您看到菜单项重复了 10 次。

    我认为您只需要使用循环来添加它们:

    foreach (ToolStripItem tsItem in contextMenuStripTableLayout.Items)
        imageControl.ContextMenuStrip.Items.Add(tsItem);
    

    【讨论】:

    • 感谢格兰特。我还有一个问题。请参阅上面的编辑版本。
    • 如果我采取第二个想法,那么我得到了 CS1503 错误。无法将“ContextMenuStrip”转换为“菜单”。
    • imageControl.contextMenuStrip.Items.Add(tsItem);正确的 ?但是,我在这段代码中收到了一些关于 InvalidOperationException 的错误消息。我该怎么办 ?你能看到我的新更新版本吗?
    猜你喜欢
    • 1970-01-01
    • 2015-01-09
    • 2022-04-25
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 2015-02-17
    • 1970-01-01
    相关资源
    最近更新 更多