>I have a TreeView that uses a ContextMenu. What I need to do it Enable/Disable certain items in the ContextMenu based on what is selected in teh TreeView. Problem is, that I am finding it difficult to find where the proper place is to place my code. When the user Right-Clicks, the ContectMenu pops up before my code to Enable/Disable its items can run. I tried TreeView_AfterSelect, TreeView_BeforeSelect, TreeView_Click, TreeView_MouseUp and ContectMenu_PopUp.
Ben,
This is what I do in one of my treeviews: I use the MouseDown event, something like this
private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
TreeNode treenode = this.treeView1.GetNodeAt(e.X, e.Y);
if (treenode != null)
{
if (e.Button == MouseButtons.Right)
{
this.PrepareContextMenu(treenode);
}
}
}
In the PrepareContextMenu() method I enable/disable the context menu items. I haven't had any problems with this approach in either 1.1 or 2.0.
In 2.0 (I don't know if it is there in 1.1) the ContextMenuStrip has an Opening event which I sometimes use to prepare the contextmenu before opening.
Hope this helps you.
Einar
Semper ubi sub ubi.