ToolCommun.Clone()
Public Class ContextMenuStripTreeNode Inherits ContextMenuStrip 'Le Context menu strip possède en "permanence" ces deux items Private WithEvents ToolCommun As New ToolStripMenuItem("Commun") Private WithEvents ToolMagic As New ToolStripMenuItem("Magic") Public Sub New() Me.InitItems() End Sub 'Mise à diposition du noeud cliqué Private _node As TreeNode Public ReadOnly Property Node() As TreeNode Get Return _node End Get End Property 'Affichage personnalisé Public Overloads Sub Show(ByVal Node As TreeNode) 'On stoke le noead _node = Node 'On rattache le ContextStrip au noeud '(facilite le positionnement du menu) _node.ContextMenuStrip = Me 'Initialisation des items Me.InitItems() 'Test le type de noeud If _node.Text = "Noeud1" Then 'Possibilité d'ajouter des noeuds dynamiques Dim item As New ToolStripMenuItem(_node.Name) Me.Items.Add(item) AddHandler item.Click, AddressOf Tool_Click ElseIf _node.Text = "Noeud3" Then 'Affichage spécifique d'item permanent ToolMagic.Visible = True End If 'Affichage du menu Me.Show() End Sub 'Initialisation des items Public Sub InitItems() Me.Items.Clear() Me.Items.Add(ToolCommun) Me.Items.Add(ToolMagic) ToolMagic.Visible = False End Sub Private Sub ToolCommun_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolCommun.Click MessageBox.Show("Je suis le commun du noeud " & _node.Text) End Sub Private Sub ToolMagic_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolMagic.Click MessageBox.Show("Je suis le magic du noeud " & _node.Text) End Sub Private Sub Tool_Click(ByVal sender As Object, ByVal e As System.EventArgs) MessageBox.Show("Je suis le tool perso du noeud " & _node.Text) End Sub End Class
Public Class Form1 Private WithEvents cms As New ContextMenuStripTreeNode() Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick If e.Button = Windows.Forms.MouseButtons.Right Then cms.Show(e.Node) End If End Sub End Class