Find all ContextMenuStrips defined on a Windows Form (not necessarily attached at runtime)
In my winforms project, some of the forms have a set开发者_运维技巧 of ContextMenuStrips defined on them (through the visual studio designer).
Some of these contextmenustrips have been attached to controls, but others have not.
Now my problem is this: I need to go through all of the ContextMenuStrips at runtime, whether they are attached or not.
I've got some code that will recursively go through all controls and check the ContextMenuStrip property and this works fine.... However I can not get to the ContextMenuStrips that haven't been assigned to a control yet.
ContextMenuStrip components that you drop on a form with the designer are added to the "components" collection. You can find them back by iterating it:
For Each co As System.ComponentModel.Component In Me.components.Components
If TypeOf co Is ContextMenuStrip Then
Dim cms = DirectCast(co, ContextMenuStrip)
'' do something
End If
Next
精彩评论