我的 excel 加载项中有一个 winform 对话框,单击面板按钮会弹出该对话框。我在工作表中添加了一个选择更改事件。该事件不是第一次被触发。我必须关闭对话框并再次打开它,这一次它会起作用。我在这里遗漏了什么,或者它是 excel interop API 的错误?
环境:Excel 2007,.NET 4.0,互操作运行时:v1.1.4322
以下是代码
public partial class CreateColumn : Form
{
public CreateColumn()
{
InitializeComponent();
Excel.Worksheet ws = Globals.ThisAddIn.Application.ActiveSheet;
//Bug: this event does not fire the first time.. works on second time.
ws.SelectionChange += new Excel.DocEvents_SelectionChangeEventHandler(ColRangeSelChange);
}
public void ColRangeSelChange(Excel.Range target)
{
System.Windows.Forms.MessageBox.Show(target.AddressLocal);
}}
这就是创建列的调用方式
private void smartTemplateBtn_Click(object sender, EventArgs e)
{
Range SelectedRange = Globals.ThisAddIn.Application.Selection;
if (SelectedRange != null)
{
List<string> DataSetLabels = new List<string>();
foreach (Range cell in SelectedRange.Cells)
{
if (cell.Value2 != null && !cell.Value2.Equals(""))
{
if (!DataSetLabels.Contains(cell.Value2))
{
DataSetLabels.Add(cell.Value2);
}
}
}
if (DataSetLabels.Count > 0)
{
PopupCreateColumnDialog(DataSetLabels);
}
}
}
public void PopupCreateColumnDialog(List<string> DataSetLabels)
{
if (DataSetLabels.Count > 0)
{
CreateColumn colDialog = new CreateColumn();
colDialog.TopMost = true;
colDialog.Show();
}
}