Bill Kinney

Hi,

I have a VSTO document with a TreeView in the ActionsPane. I am attempting to setup the ContextMenuStrip for the TreeNodes but I have run into a problem. When I right-click on one TreeNode to get the ContextMenuStrip and then left-click on a different TreeNode, the previous ContextMenuStrip will not disappear. The same code works just fine in a standard WinForm application, however. Is this a bug or am I doing something stupid Any workaround suggestions

Thanks.

Code Snippet

TreeView tv = new TreeView();

TreeNode node1 = new TreeNode("one");

TreeNode node2 = new TreeNode("two");

ContextMenuStrip cms = new ContextMenuStrip();

cms.Items.Add("item");

node1.ContextMenuStrip = cms;

tv.Nodes.Add(node1);

tv.Nodes.Add(node2);

//use for WinForm app

this.Controls.Add(tv);

//use for VSTO doc

Globals.ThisDocument.ActionsPane.Controls.Add(tv);



Re: Visual Studio Tools for Office ContextMenuStrip won't disappear when clicking another TreeNode in VSTO

Ji Zhou – MSFT

Hi Bill,

As a work around, I suggest that you close the ContextMenuStrip in the Mouse click event handle. Codes are like these:

Code Snippet

ContextMenuStrip cms = null;

private void ThisWorkbook_Startup(object sender, System.EventArgs e)

{

TreeView tv = new TreeView();

TreeNode node1 = new TreeNode("one");

TreeNode node2 = new TreeNode("two");

cms = new ContextMenuStrip();

cms.Items.Add("item");

node1.ContextMenuStrip = cms;

tv.Nodes.Add(node1);

tv.Nodes.Add(node2);

tv.MouseClick += new MouseEventHandler(tv_MouseClick);

this.ActionsPane.Controls.Add(tv);

}

void tv_MouseClick(object sender, MouseEventArgs e)

{

cms.Close();

}

Thanks

Ji






Re: Visual Studio Tools for Office ContextMenuStrip won't disappear when clicking another TreeNode in VSTO

Bill Kinney

Thanks for the response.

Your suggestion helps to some degree, however, it doesn't solve all of the oddities with the menu.

I've found that the following works well...

Code Snippet

ContextMenuStrip cms = new ContextMenuStrip();

private void ThisDocument_Startup(object sender, System.EventArgs e)

{

TreeView tv = new TreeView();

TreeNode node1 = new TreeNode("one");

TreeNode node2 = new TreeNode("two");

ToolStripItem tsi = cms.Items.Add("item");

node1.ContextMenuStrip = cms;

tv.Nodes.Add(node1);

tv.Nodes.Add(node2);

Globals.ThisDocument.ActionsPane.Controls.Add(tv);

tv.Leave += new EventHandler(tv_Leave);

tv.MouseDown += new MouseEventHandler(tv_MouseDown);

}

void tv_MouseDown(object sender, MouseEventArgs e)

{

cms.Close();

}

void tv_Leave(object sender, EventArgs e)

{

cms.Close();

}