本文介绍了Excel 2013中的动态禁用/启用自定义上下文菜单不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel AddIn。 我在单元格上下文菜单中添加了2个上下文菜单项。

I have an Excel AddIn.  I add 2 context menu items in cell context menu.

当您右键单击一个单元格时,根据单元格的公式,将禁用一个上下文菜单项。

When you right click a cell, based on the formula of the cell, one context menu item will be disabled.

我在sheetSelectionChangeEvent中执行了此操作

I did this in sheetSelectionChangeEvent

这在Excel 2003,2007和2010中工作正常,但它在Excel 2013中不起作用。

This works fine in Excel 2003, 2007 and 2010 but it does not work in Excel 2013.

请帮忙。谢谢

        private void ApplicationSheetSelectionChange(COMObject sh,Range target)

        {

               DisableMenubarsButtonsWRibbon(XLApp.Selection as Range);

        }

        private void ApplicationSheetSelectionChange(COMObject sh, Range target)
        {
               DisableMenubarsButtonsWRibbon(XLApp.Selection as Range);
        }

public void DisableMenubarsButtonsWRibbon(Range rng)

{

  var formula = rng.Formula as string;

  if(formula is function1)

  {

   _contextMenuItem1.Enabled = true;

   _contextMenuItem2.Enabled = false;

 }

  else if(formula is function2)

  {

   _contextMenuItem1.Enabled = false;

   _contextMenuItem2.Enabled = true;

 }

  else

  {

   _contextMenuItem1.Enabled = true;

   _contextMenuItem2。 Enabled = true;

 }

}

public void DisableMenubarsButtonsWRibbon(Range rng)
{
 var formula = rng.Formula as string;
 if(formula is function1)
 {
  _contextMenuItem1.Enabled = true;
  _contextMenuItem2.Enabled = false;
 }
 else if(formula is function2)
 {
  _contextMenuItem1.Enabled = false;
  _contextMenuItem2.Enabled = true;
 }
 else
 {
  _contextMenuItem1.Enabled = true;
  _contextMenuItem2.Enabled = true;
 }
}

Peace&Joy

Peace & Joy

推荐答案

如何添加上下文菜单?我为Excel 2013编写了一个示例来演示如何启用和禁用下面的上下文菜单按钮,每次更改工作表时,如果禁用该按钮将启用该按钮,反之亦然:

How do you add the context menu? I wrote a sample for Excel 2013 to demonstrate how to enable and disable the context menu button like below, every time I change the sheet, the button will enable if it is disable and vice versa:

  private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Application.SheetChange += Application_SheetChange;
            CommandBarControl cbc1 = AddCellContextButton("button1");
        }

        void Application_SheetChange(object Sh, Excel.Range Target)
        {
            CommandBar cellbar = Application.CommandBars["Cell"];
            foreach (CommandBarControl cbc in cellbar.Controls)
            {
                if (cbc.Caption == "button1")
                    cbc.Enabled = !cbc.Enabled;
            }
        }
   private CommandBarControl AddCellContextButton(string caption)
        {
            CommandBar cellbar = Application.CommandBars["Cell"];
            CommandBarControl cbc1 = cellbar.Controls.Add(MsoControlType.msoControlButton);
            CommandBarButton btn1 = (cbc1 as CommandBarButton);
            btn1.Caption = "button1";
            return cbc1;
        }

你介意分享你的小样本来重现这个问题吗?您可以通过
更新它。

Would you mind sharing your small sample to reproduce the issue? You can update it throughSkydrive.

祝你好运

Fei


这篇关于Excel 2013中的动态禁用/启用自定义上下文菜单不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 01:16