我有一个工具条。对于此工具条,我正在添加ToolStripSplitButton
,对于此ToolStripSplitButton
,我正在添加工具条项目,包括ToolStripSeparator
。在工具条项目的点击事件中,我使用下面的代码从ToolStripSplitButton
下拉列表中检索项目。
ToolStripDropDown tditems = ((System.Windows.Forms.ToolStripDropDownItem)(items[0])).DropDown;
foreach (ToolStripMenuItem item in tditems.Items)
{
//something here
}
由于下拉项目在运行时同时包含工具条项和ToolStripSeparator
,因此会出现以下错误。
其他信息:无法投射 对象类型 'System.Windows.Forms.ToolStripSeparator' 输入 'System.Windows.Forms.ToolStripMenuItem'。
有人能帮助我吗?
由于
答案 0 :(得分:6)
如果您使用的是.NET 3.5,则可以使用OfType
扩展方法,如下所示。
foreach (var item in tditems.Items.OfType<ToolStripMenuItem>())
{
// something here
}