Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c将自定义命令栏菜单添加到特定excel表_C#_Excel_Office Interop - Fatal编程技术网

C# 使用c将自定义命令栏菜单添加到特定excel表

C# 使用c将自定义命令栏菜单添加到特定excel表,c#,excel,office-interop,C#,Excel,Office Interop,我在表右键单击中有一个自定义命令栏菜单,但我需要它特定于一个表,并且不希望它显示在任何其他表上。这可能吗? 若否,有何其他建议? editSourceBtn = (Office.CommandBarButton)tableCommandBar.Controls.Add(1, missing, missing, missing, missing); editSourceBtn.Style = Office.MsoButtonStyle.msoButtonCaption; e

我在表右键单击中有一个自定义命令栏菜单,但我需要它特定于一个表,并且不希望它显示在任何其他表上。这可能吗? 若否,有何其他建议?

    editSourceBtn = (Office.CommandBarButton)tableCommandBar.Controls.Add(1, missing, missing, missing, missing);
    editSourceBtn.Style = Office.MsoButtonStyle.msoButtonCaption;
    editSourceBtn.Caption = "Edit Source";
    editSourceBtn.Tag = "Edit Source";
    editSourceBtn.Click +=new Office._CommandBarButtonEvents_ClickEventHandler(editSourceBtn_Click);

是的,这是可能的。但事实是命令栏不再使用了。他们被否决了。您需要改用Fluent UI控件。您可以使用回调,例如getEnabled或getVisible,必要时可以调用IRibbonUI接口的或InvalidateControl方法来调用回调

Dim MyRibbon As IRibbonUI
Sub MyAddInInitialize(Ribbon As IRibbonUI)
    Set MyRibbon = Ribbon
End Sub
Sub myFunction()
    // Invalidates the caches of all of this add-in’s controls 
    MyRibbon.Invalidate()            
End Sub
您可以在MSDN中的以下系列文章中阅读有关Ribbon UI的更多信息:


我找到了解决方案,我在一个特定的选项卡中有一个表,并且想要右键单击上下文,在if语句中指定它,如果该表是我需要的,并且所选范围与它添加的右键单击按钮的表范围相交

private void Application_SheetBeforeRightClick(object Sh, Excel.Range Target, ref bool Cancel)
    {
        Excel.Range ResourceTable;
        Excel.Range IntersectRange;

        foreach (Office.CommandBarControl ctrl in tableCommandBar.Controls)
        {
            if (ctrl.Tag == "Edit" || ctrl.Tag == "Delete") ctrl.Delete();
        }
        editBtn = null;

        if (this.Application.ActiveSheet.Name == "Mysheet" && this.Application.ActiveSheet.ListObjects.Count > 0)  
        {

            ResourceTable = this.Application.ActiveWorkbook.Worksheets["Mysheet"].ListObjects["myTable"].Range;
            IntersectRange = this.Application.Intersect(ResourceTable, Target);

            if (IntersectRange != null & ResourceTable.Rows.Count > 2 && editSourceBtn == null)
            {
                editBtn = (Office.CommandBarButton)tableCommandBar.Controls.Add(1, missing, missing, missing, missing);
                editBtn.Style = Office.MsoButtonStyle.msoButtonCaption;
                editBtn.Tag = "Edit";
                editBtn.Click += new Office._CommandBarButtonEvents_ClickEventHandler(editSourceBtn_Click);

            }
        }
    }

希望这有帮助。

这会在功能区中添加一个按钮,我试图避免使用它,因为该按钮将仅用于整个工作簿中的一个表,这就是为什么我认为在右键单击菜单中添加一个按钮更合适的原因。