[英]VSTO Excel.DropDown event
I am adding dynamically an Excel.DropDown within a ActiveSheet cell using C# VSTO. 我正在使用C#VSTO在ActiveSheet单元中动态添加Excel.DropDown。 Is there a way to handle a change selection event without embedding a macro to workbook?
有没有一种方法可以处理更改选择事件而无需在工作簿中嵌入宏? Alternatively I would be interested to use Excel's data validation technique (cell.Validation), where presumably I would need to work with SheetChange event.
另外,我可能会对使用Excel的数据验证技术(cell.Validation)感兴趣,因为我大概需要使用SheetChange事件。 Not sure which one is more efficient?
不确定哪个更有效? The code I am using is bellow
我正在使用的代码如下
var currentSheet = Application.Sheets[strDestSheetName];
var inv = Application.Sheets[strSrcSheetName];
var items = inv.Range[strSrcRange];
var list_items = new List<string>();
foreach (Excel.Range cell in items)
{
list_items.Add(cell.Value2.ToString());
}
Range xlsRange;
xlsRange = currentSheet.Range[strDestCell];
Excel.DropDowns xlDropDowns;
Excel.DropDown xlDropDown;
xlDropDowns = ((Excel.DropDowns)(currentSheet.DropDowns(Missing.Value)));
xlDropDown = xlDropDowns.Add((double)xlsRange.Left, (double)xlsRange.Top, (double)xlsRange.Width, (double)xlsRange.Height, true);
//Add item into drop down list
for (int i = 0; i < list_items.Count; i++)
{
xlDropDown.AddItem(list_items[i], i + 1);
}
xlDropDown.OnAction = "SomeMacroCode";
you can use Excel's data validation technique in this way: 您可以通过以下方式使用Excel的数据验证技术:
var activeSheet = (Worksheet) Globals.ThisAddIn.Application.ActiveSheet;
int lastUsedCell = activeSheet.UsedRange.Rows.Count;
//in this example we dynamicly add drop down list to second colomn
string columnName = "B" + lastUsedCell;
//the range is from second colomn of first row to last row
Range range = activeSheet.Range["B1", columnName];
var list=new List<string>();
list.Add("a");
list.Add("b");
string items= string.Join(",",
list);
range.Validation.Add(XlDVType.xlValidateList, Type.Missing,
XlFormatConditionOperator.xlBetween, items);
InsertingTypeNotificationLable.Visible = true;
SendButton.Enabled = true;
also you can have dynamicly populated drop down list at run time, for example whenever user click a button in your task pane 您还可以在运行时动态填充下拉列表,例如,每当用户单击任务窗格中的按钮时
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.