ToolStripSeparator 的问题

发布于 2024-08-07 00:54:04 字数 1308 浏览 3 评论 0原文

我有一个工具条。对于此工具条,我添加 ToolStripSplitButton< /code>并为此 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'。

有人可以帮助我吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2024-08-14 00:54:04

如果您使用的是 .NET 3.5,则可以使用 OfType扩展方法如下。

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文