问题描述
我发现,虽然试图找到一个项目的拥有的工具条,这不是一个容易,因为仅仅着眼于项目的所有者。下面是一些伪code,我觉得应该工作。任何人都可以看到任何与此问题还是应该适用于所有情况下?
I found that while trying to find the owning toolstrip of an item, it wasn't a easy as just looking at the owner of the item. Below is some pseudo code that I think should work. Can anyone see any problems with this or should it work in all cases?
(1)是Item.Owner的类型的ToolStrip?
(1) Is type of Item.Owner a ToolStrip?
(2)是的,返回Item.Owner
(2) Yes, Return Item.Owner
(3)不,项目= Item.OwnerItem。转到(1)。
(3) No, Item = Item.OwnerItem. Go to (1).
ETA:
我想使测试的一般测试。因此,而不是测试的ToolStrip的,我应该测试为的ToolStrip,MenuStrip中,StatusStrip中或的ContextMenuStrip。
I'd like to make the test a general test. So instead of testing for ToolStrip, I should be testing for ToolStrip, MenuStrip, StatusStrip or ContextMenuStrip.
是什么让上述不同的其他的ToolStrip提到的4个来源,如ToolStripDropDown,ToolStripDropDownMenu和ToolStripOverflow控制?
What makes the 4 mentioned above different to other ToolStrip derived controls such as ToolStripDropDown, ToolStripDropDownMenu and ToolStripOverflow?
ETA2:忽略,绝对鲤鱼
ETA2: Ignore, absolute carp!
据一个我可以告诉大家,这件事情做Control.TopLevelControl。上面说的3控件不能被添加到形式返回自己的TopLevelControl。 4有效的控制返回没有为TopLevelControl,被添加到窗体之前,然后窗体本身,添加后。击>
推荐答案
这工作:
ToolStrip owner = testToolStripMenuItem.Owner;
while (owner is ToolStripDropDownMenu)
owner = (owner as ToolStripDropDownMenu).OwnerItem.Owner;
这篇关于正确的方式来获得一个工具条项目的所有者工具条在.NET中,的WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!