C# excel addin如何实现Ribbon上一个按钮显示一个UserControl?

private void button1_Click(object sender, RibbonControlEventArgs e)

{
UserControl1 uc = new UserControl1();
uc.Show();
}

这样写编译没错成功运行,但是点击按钮后没有反应,求大神赐教
已解决,参考了下面链接的博客,感谢博主
http://bbs.51cto.com/thread-1019619-1.html

第1个回答  2015-06-05
得把uc 添加到CustomTaskPaneCollection 中才行
给你一个参考:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Tools;
namespace FinWordHelper
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.DocumentOpen += Application_DocumentOpen;
this.Application.DocumentChange += Application_DocumentChange;
}
void Application_DocumentChange()
{
FinPaneManager.Instance.CollectWordPanes();
}

void Application_DocumentOpen(Word.Document Doc)
{
FinPaneManager.Instance.CollectWordPanes();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO 生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}

#endregion
}
#region FinPaneManager
public class FinPaneManager
{
private FinPaneManager()
{
custPanes = Globals.ThisAddIn.CustomTaskPanes;
}
private static FinPaneManager m_Instance = new FinPaneManager();
public static FinPaneManager Instance
{
get { return m_Instance; }
}
private CustomTaskPaneCollection custPanes;
public CustomTaskPane GetFinWordFormatPane()
{
Word.Window window = Globals.ThisAddIn.Application.ActiveWindow;
foreach (CustomTaskPane ctp in custPanes)
{
if (ctp.Title == "Fin格式化工具" && ctp.Window == window)
{
return ctp;
}
}
var cp = custPanes.Add(
new WordFormatter(),
"Fin格式化工具",
window
);
cp.Width = 250;
return cp;
}
public CustomTaskPane GetFinWordCheckPane()
{
Word.Window window = Globals.ThisAddIn.Application.ActiveWindow;
foreach (CustomTaskPane ctp in custPanes)
{
if (ctp.Title == "拼写检查" && ctp.Window == window)
{
return ctp;
}
}
var cp = custPanes.Add(
new WordSpellCheck(),
"拼写检查",
window
);
cp.Width = 250;
return cp;
}
public void CollectWordPanes()
{
for (int i = custPanes.Count - 1; i >= 0; i--)
{
if (custPanes[i].Window == null)
{
custPanes.RemoveAt(i);
}
}
}
}
#endregion
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
namespace FinWordHelper
{
public partial class CustomRibbon //按钮调用在这里
{
private void CustomRibbon_Load(object sender, RibbonUIEventArgs e)
{
}
private void button1_Click(object sender, RibbonControlEventArgs e)
{
FinPaneManager.Instance.GetFinWordFormatPane().Visible = true;
}
private void button2_Click(object sender, RibbonControlEventArgs e)
{
FinPaneManager.Instance.GetFinWordCheckPane().Visible = true;
}
}
}
相似回答