Context Menu in TreeView control
-
I'm displaying a TreeView of an active directory domain. I want to right-click on a Node and display a context menu which has a submenu which is built depending on the type of object clicked on. I've created a context menu but when right-clicking on an object, this code, running in the Popup event of the context menu item which displays the sub-menu:
MenuItem directoryObjectClickedOn = (MenuItem)sender; ContextMenu cm = directoryObjectClickedOn.GetContextMenu(); TreeView ad = (TreeView)cm.SourceControl; DirectoryEntry aDObject = (DirectoryEntry)ad.SelectedNode.Tag; MessageBox.Show(aDObject.Name);
always gives me the previous object, not the current one. Any idea how to get the current object? -
I'm displaying a TreeView of an active directory domain. I want to right-click on a Node and display a context menu which has a submenu which is built depending on the type of object clicked on. I've created a context menu but when right-clicking on an object, this code, running in the Popup event of the context menu item which displays the sub-menu:
MenuItem directoryObjectClickedOn = (MenuItem)sender; ContextMenu cm = directoryObjectClickedOn.GetContextMenu(); TreeView ad = (TreeView)cm.SourceControl; DirectoryEntry aDObject = (DirectoryEntry)ad.SelectedNode.Tag; MessageBox.Show(aDObject.Name);
always gives me the previous object, not the current one. Any idea how to get the current object?In the MouseDown/Up event, get the x and y coordinates of the cursor. From this you can determine which node the mouse is on by using the GetNodeAt() method. If you want to know if a mouse has selected the tree view item label or its icon. I hope this helps. Human beings were not meant to sit in little cubicles staring at computer screens all day, filling out useless forms and listening to eight different bosses drone on about about mission statements. -- Peter Gibbons
-
In the MouseDown/Up event, get the x and y coordinates of the cursor. From this you can determine which node the mouse is on by using the GetNodeAt() method. If you want to know if a mouse has selected the tree view item label or its icon. I hope this helps. Human beings were not meant to sit in little cubicles staring at computer screens all day, filling out useless forms and listening to eight different bosses drone on about about mission statements. -- Peter Gibbons
Thanks very much. That sorted it out for me and I'm generating a context-specific menu.