如何在创建时使Excel CommandBarButton不可见?

时间:2012-03-09 18:22:40

标签: c# excel-addins commandbar

我有一个Excel加载项,它有2个按钮,第二个加载加载项时我想隐藏它。这是我的代码:

public void OnStartupComplete(ref System.Array custom)
{
    object omissing = System.Reflection.Missing.Value;                

    CommandBarButton Button1 = (CommandBarButton)g_PLCToolBarInstance.Controls.Add(1, omissing, omissing, omissing, omissing);
    Button1 .Visible = true;
    Button1 .Enabled = true;
    Button1 .Caption = "Button1";                
    Button1 .Style = MsoButtonStyle.msoButtonIcon;
    Button1 .Picture = PictureDispConverter.ToIPictureDisp(OneIco);

    CommandBarButton Button2 = (CommandBarButton)g_PLCToolBarInstance.Controls.Add(1, omissing, omissing, omissing, omissing);
    Button2 .Visible = false;
    Button2 .Enabled = false;
    Button2 .Caption = "Button2";
    Button2 .Style = MsoButtonStyle.msoButtonIcon;
    Button2 .Picture = PictureDispConverter.ToIPictureDisp(TwoIco);

    ....
}

问题是第二个按钮被绘制,然后被删除(而不是从开始隐藏),在工具栏选项下拉列表中留下了TwoIco的重影...

Bad

而不是......

Good

请问任何想法?!

1 个答案:

答案 0 :(得分:2)

我有一个旧的加载项,它在OnStartupComplete中创建了按钮,并查看代码,我的唯一实质性区别是我在设置标题,样式和图片属性后设置了.Visible = false