C# VSTO Excel.DropDown事件

C# VSTO Excel.DropDown事件,c#,excel,vsto,vba,C#,Excel,Vsto,Vba,我正在使用C#VSTO在ActiveSheet单元格中动态添加一个Excel.DropDown。有没有一种方法可以在不将宏嵌入工作簿的情况下处理更改选择事件?或者,我会有兴趣使用Excel的数据验证技术(cell.validation),在这里我可能需要处理SheetChange事件。不确定哪一个更有效? 我使用的代码如下 var currentSheet = Application.Sheets[strDestSheetName]; var inv = Application.Sheets[s

我正在使用C#VSTO在ActiveSheet单元格中动态添加一个Excel.DropDown。有没有一种方法可以在不将宏嵌入工作簿的情况下处理更改选择事件?或者,我会有兴趣使用Excel的数据验证技术(cell.validation),在这里我可能需要处理SheetChange事件。不确定哪一个更有效? 我使用的代码如下

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";
var currentSheet=Application.Sheets[strDestSheetName];
var inv=应用程序表[STRCSheetName];
var项目=投资范围[STRRCRANGE];
var list_items=新列表();
foreach(项目中的Excel.Range单元格)
{
list_items.Add(cell.Value2.ToString());
}
范围xlsRange;
xlsRange=当前表范围[strDestCell];
Excel.DropDowns-xlDropDowns;
Excel.DropDown-xlDropDown;
xlDropDowns=((Excel.DropDowns)(currentSheet.DropDowns(Missing.Value));
xlDropDown=xlDropDowns.Add((双)xlsRange.Left,(双)xlsRange.Top,(双)xlsRange.Width,(双)xlsRange.Height,true);
//将项目添加到下拉列表中
对于(int i=0;i
您可以通过以下方式使用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;
var-activeSheet=(工作表)Globals.ThisAddIn.Application.activeSheet;
int lastUsedCell=activeSheet.UsedRange.Rows.Count;
//在本例中,我们动态地将下拉列表添加到第二列
字符串columnName=“B”+lastUsedCell;
//范围是从第一行的第二列到最后一行
Range Range=activeSheet.Range[“B1”,columnName];
var list=新列表();
列表。添加(“a”);
列表。添加(“b”);
string items=string.Join(“,”,
名单);
range.Validation.Add(XlDVType.xlValidateList,Type.Missing,
XlFormatConditionOperator.xlBetween,项目);
InsertingTypeNotificationLable.Visible=true;
SendButton.Enabled=true;

此外,您还可以在运行时动态填充下拉列表,例如每当用户单击任务窗格中的按钮时

我同意您的方法,但不清楚如何处理更改值事件。我宁愿避免使用sheet_change事件,因为逗号并不总是正确的分隔符。您应该改用
System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator