We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

TreeViewAdv and ContextMenuStrip

I have a treeviesadv with a contextmenustrip (in a GroupBar). 1) When the user right-clicks on an empty space (not a node) the node closest to that horizontal point is somehow selected. I have tried getting the point from the Control.MousePosition then attempting to select the node at that position to check if the node is null. But, no matter where the user clicks on the empty space (as long as a node is horizontal to that click) a node is selected. I am trying to NOT show the context menu when an empty space is right-clicked (look at outlook 2003). 2) When a user right-clicks (same GroupBar, same GroupBar item) on another panel (not the panel containing the tree, which the context menustrip is in) the context menu still pops up. If a contextmenu is "assigned" to a tree, why would it show up when something other than the tree "area" is right-clicked? Thanks, Rich

6 Replies

VS Vijayanand S Syncfusion Team January 27, 2006 11:05 AM UTC

Hi Rich, 1. You could use the following code snippet in context menu opening event to avoid showing context menu if we click on empty space on the treeview: TreeNodeAdv node = this.treeViewAdv1.GetNodeAtPoint(this.treeViewAdv1.LastMousePositionToClient()); if (node == null) e.Cancel = true; else if (!node.TextBounds.Contains(this.treeViewAdv1.LastMousePositionToClient())) e.Cancel = true; I have illustrates this behavior in the attached sample also. 2. I am afraid, I was not able to reproduce the problem. Could you please modify the attached sample to reproduce the problem? It will help us to resolve the issue as soon as possible. Please take a look at the sample and let me know if you have any questions. Thanks for using Syncfusion Products. Thanks, Vijay Test Sample


GR Gordan Redzic January 27, 2006 03:26 PM UTC

Hi Vijay, Thanks for the quick reply and sample. Your sample solved the problem of the context menu opening when user clicks on white space. But, a node is still selected when user right clicks on white space that is horizontal to a node. I want a node to only be selected if a user selects a node. Forget about #2, it was an error in my code. >Hi Rich, > >1. You could use the following code snippet in context menu opening event to avoid showing context menu if we click on empty space on the treeview: > > TreeNodeAdv node = this.treeViewAdv1.GetNodeAtPoint(this.treeViewAdv1.LastMousePositionToClient()); > if (node == null) > e.Cancel = true; > else if (!node.TextBounds.Contains(this.treeViewAdv1.LastMousePositionToClient())) > e.Cancel = true; > >I have illustrates this behavior in the attached sample also. > >2. I am afraid, I was not able to reproduce the problem. Could you please modify the attached sample to reproduce the problem? It will help us to resolve the issue as soon as possible. > >Please take a look at the sample and let me know if you have any questions. Thanks for using Syncfusion Products. > >Thanks, >Vijay > >Test Sample


VS Vijayanand S Syncfusion Team January 27, 2006 04:26 PM UTC

Hi Rich, Thanks for the update. Please try with the following code to avoid selection when you right click on the TreeViewAdv: private void treeViewAdv1_MouseDown(object sender, MouseEventArgs e) { this.treeViewAdv1.BeginUpdate(); if(this.treeViewAdv1.RMouseDownNode!=null) if (e.Button == MouseButtons.Right && this.treeViewAdv1.RMouseDownNode.TextBounds.Contains(this.treeViewAdv1.LastMousePositionToClient())) { this.treeViewAdv1.SelectedNode = this.treeViewAdv1.RMouseDownNode; } } private void treeViewAdv1_MouseUp(object sender, MouseEventArgs e) { this.treeViewAdv1.EndUpdate(true); } Please take a look at the attached modified sample which illustrates the above functionality and let me know if this resolves your requirement. Thanks, Vijay Modified Sample


GR Gordan Redzic January 30, 2006 10:46 PM UTC

Unfortunately, this causes the nodes (parent nodes) to not open and close consistently. Look at you sample and try to open close the nodes using the mouse buttons. >Hi Rich, > >Thanks for the update. > >Please try with the following code to avoid selection when you right click on the TreeViewAdv: > > private void treeViewAdv1_MouseDown(object sender, MouseEventArgs e) > { > > this.treeViewAdv1.BeginUpdate(); > if(this.treeViewAdv1.RMouseDownNode!=null) > if (e.Button == MouseButtons.Right && this.treeViewAdv1.RMouseDownNode.TextBounds.Contains(this.treeViewAdv1.LastMousePositionToClient())) > { > this.treeViewAdv1.SelectedNode = this.treeViewAdv1.RMouseDownNode; > } > } > > private void treeViewAdv1_MouseUp(object sender, MouseEventArgs e) > { > this.treeViewAdv1.EndUpdate(true); > } > > >Please take a look at the attached modified sample which illustrates the above functionality and let me know if this resolves your requirement. > >Thanks, >Vijay > >Modified Sample


VS Vijayanand S Syncfusion Team January 31, 2006 10:55 AM UTC

Hi Rich, Sorry for the inconvenience. Please replace the following code in the MouseDown event. Improper place of BeginUpdate method caused the painting problem. if (e.Button == MouseButtons.Right) { this.treeViewAdv1.BeginUpdate(); if (this.treeViewAdv1.RMouseDownNode != null) if (this.treeViewAdv1.RMouseDownNode.TextBounds.Contains(this.treeViewAdv1.LastMousePositionToClient())) { this.treeViewAdv1.SelectedNode = this.treeViewAdv1.RMouseDownNode; } } Please let me know if you still face this problem. Thanks, Vijay


GR Gordan Redzic January 31, 2006 01:29 PM UTC

Hi Vijay, That did it, thanks for all your help! >Hi Rich, > >Sorry for the inconvenience. > >Please replace the following code in the MouseDown event. Improper place of BeginUpdate method caused the painting problem. > >if (e.Button == MouseButtons.Right) > { > this.treeViewAdv1.BeginUpdate(); > if (this.treeViewAdv1.RMouseDownNode != null) > if (this.treeViewAdv1.RMouseDownNode.TextBounds.Contains(this.treeViewAdv1.LastMousePositionToClient())) > { > this.treeViewAdv1.SelectedNode = this.treeViewAdv1.RMouseDownNode; > } > } > >Please let me know if you still face this problem. > >Thanks, >Vijay

Loader.
Live Chat Icon For mobile
Up arrow icon