皆さんとメリークリスマスによくお越しください。ToolStripMenuItemにAddHandlerをContextMenuStrip.Openingイベントが動作していない
私は最近VBプロジェクトを継承し、機能を追加する必要があります。だから、私は以下のコードを持っています:
Private Sub AddItems()
Dim itemMenu = DirectCast(ContextMenuStrip.Items.Find("name", False)(0), ToolStripMenuItem)
For Each dbObject In dbObjects
Dim item As New ToolStripMenuItem(dbObject.Name)
item.Tag = dbObject
AddHandler item.Click, AddressOf Item_Click
itemMenu.DropDownItems.Add(item)
Next
End Sub
Private Sub RemoveItems()
Dim itemMenu = DirectCast(ContextMenuStrip.Items.Find("name", False)(0), ToolStripMenuItem)
For Each item As ToolStripItem In itemMenu.DropDownItems
RemoveHandler item.Click, AddressOf Item_Click
Next
itemMenu.DropDownItems.Clear()
End Sub
Private Sub ContextMenuStrip_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip.Opening
AddItems()
End Sub
Private Sub ContextMenuStrip_Closing(sender As Object, e As ToolStripDropDownClosingEventArgs) Handles ContextMenuStrip.Closing
RemoveItems()
End Sub
Private Sub Item_Click(sender As Object, e As EventArgs)
' Do the work
End Sub
ロジックは、有効なデータベースオブジェクトを毎回コンテキストメニューのサブメニューを埋めることです。
問題は、このコードが機能しないことです。コンテキストメニューにアイテムを完全に追加しますが、AddHandler item.Click, AddressOf Item_Click
は何もしません。
私がAddItems()
をと呼ぶと、それは完璧に機能します。
ご協力いただければ幸いです。 WozzeC後
2016年12月22日ソリューションは、私が今しながら、少しのためにこれで遊んでてきたと私はあなたの問題を再現するために管理している
Private Sub AddItems()
RemoveItems()
Dim itemMenu = DirectCast(ContextMenuStrip.Items.Find("name", False)(0), ToolStripMenuItem)
For Each dbObject In dbObjects
Dim item As New ToolStripMenuItem(dbObject.Name)
item.Tag = dbObject
AddHandler item.Click, AddressOf Item_Click
itemMenu.DropDownItems.Add(item)
Next
End Sub
Private Sub RemoveItems()
Dim itemMenu = DirectCast(ContextMenuStrip.Items.Find("name", False)(0), ToolStripMenuItem)
For Each item As ToolStripItem In itemMenu.DropDownItems
RemoveHandler item.Click, AddressOf Item_Click
Next
itemMenu.DropDownItems.Clear()
End Sub
Private Sub ContextMenuStrip_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip.Opening
AddItems()
End Sub
Private Sub Item_Click(sender As Object, e As EventArgs)
' Do the work
End Sub
'ToolStripMenuItem'コンストラクタは、私が代わりにその使用をお勧めしたいので' Click'イベントハンドラを取るオーバーロードがあります。 – jmcilhinney
@jmcilhinney応答をありがとう。これはどちらもうまくいかない。まだ検索していますが、運はありません。 – shadow