problem with ToolStripSeparator

可紊 提交于 2019-12-24 19:19:26

问题


I have a toolstrip. For this toolstrip, I am adding ToolStripSplitButton and for this ToolStripSplitButton, I am adding toolstrip items, including ToolStripSeparator. In the click event of the toolstrip items I am retrieving items from the ToolStripSplitButton dropdown using below code.

 ToolStripDropDown tditems = ((System.Windows.Forms.ToolStripDropDownItem)(items[0])).DropDown;
foreach (ToolStripMenuItem item in tditems.Items)
{
//something here
}

As the dropdown items have both toolstrip items and ToolStripSeparator at runtime, it is giving following error.

Additional information: Unable to cast object of type 'System.Windows.Forms.ToolStripSeparator' to type 'System.Windows.Forms.ToolStripMenuItem'.

Can anybody help me?

Thanks


回答1:


If you are using .NET 3.5, you could use the OfType extension method as follows.

foreach (var item in tditems.Items.OfType<ToolStripMenuItem>())
{
    // something here
}


来源:https://stackoverflow.com/questions/1537394/problem-with-toolstripseparator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!