我创建了一个定义RangeSelectionTarget的接口,如下所示:
public interface IRangeSelectionTarget
{
void OnRangeSelection(Excel.Range target);
}
在另一个类(ExcelController,封装excel互操作行为)中,我有一个方法将工作表范围选择(使用鼠标选择单元格)传递给目标:
public void SelectCaptureOn(IRangeSelectionTarget target)
{
_selectionCallback += new SelectionCallback(target.OnRangeSelection);
_selectionCallback += new SelectionCallback(SelectionCallback2);
int limit = _workbook.Worksheets.Count;
for (int i = 1; i <= limit; i++)
{
Excel.Worksheet worksheet = _workbook.Worksheets.get_Item(i);
string worksheetName = worksheet.Name;
worksheet.SelectionChange
+= new Excel.DocEvents_SelectionChangeEventHandler(OnXlSelectionCallback);
}
}
目标,一个表单,实现OnRangeSelection(),如下所示,以显示单元格地址:
public void OnRangeSelection(Excel.Range target)
{
if (this.InvokeRequired)
{
RangeSelectionDelegate rsDel = new RangeSelectionDelegate(OnRangeSelection);
this.Invoke(rsDel, new object[] { target });
}
else
{
txtRange.Text = target.AddressLocal;
txtRange.Tag = target;
}
}
这很好用,除非没有。在调用SelectCaptureOn()之后,OnRangeSelection()开始在IRangeSelectionTarget上调用,然后显示所选单元格的地址。 OnRangeSelection()检查InvokeRequired以避免线程问题....或者我希望如此。
问题是,在几次成功选择显示后,行为完全停止。
任何想法为什么或从哪里开始调查?
谢谢!