开发者

Getting the highest owner of a ToolStripDropDownItem

开发者 https://www.devze.com 2023-01-03 00:21 出处:网络
I\'m currently working on a project in which at one point, the user may right click a button which brings up a contextMenuStrip. I am already able to find the owner accurately from that strip, and man

I'm currently working on a project in which at one point, the user may right click a button which brings up a contextMenuStrip. I am already able to find the owner accurately from that strip, and manipulate the button clicked as follows:

Dim myItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim cms As ContextMenuStrip = CType(myItem.Owner, ContextMenuStrip)

Dim buttonPressed As DataButton = DirectCast(cms.SourceControl, DataButton)

But now for the tricky part. Within this contextmenuStrip, I have a DropDown menu with multiple items in there. I would assume you would be able to work your way up the ladder doing casts like above in the manner of

 ToolStripDrowpDownItem > ToolStripDropDownMenu > ToolStripMenuItem > ContextMenuStrip

Unfortunately, when I try to get the sourcecontrol from this menuStrip, it return Nothing. Any ideas on how I can get the button that was pressed from this toolStripMenuItem? My current code is as follows (in which the sourceControl is Nothing)

Dim myItem As ToolStripDropDownItem = CType(sender, ToolStripDropDownItem)
Dim dropDown As ToolStripDropDownMenu = CType(myItem.Owner, ToolStripDropDownMenu)
Dim menuItem As ToolStripMenuItem = CType(dropDown.OwnerItem, ToolStripMenuItem)
Dim cms As ContextMenuStrip = CType(menuItem开发者_如何学JAVA.Owner, ContextMenuStrip)

Dim buttonPressed As DataButton = DirectCast(cms.SourceControl, DataButton)

Any ideas on how to go about doing what I did in that first method, but just working my way up from further down the ladder?


I'd suggest instead of trying to work backward to see what button was pressed and act accordingly, assign the button's behaviour when it is created.

Dim button As New ToolStripMenuItem("do something")
AddHandler button.Click, AddressOf DoSomething

Public Sub DoSomething(ByVal sender as Object, ByVal e as System.EventArgs)
    'do something
End Sub

Then you don't have to worry about it -- when it is clicked, it will do its work.

0

精彩评论

暂无评论...
验证码 换一张
取 消