TreeView contextual menu location

  • Thread starter Thread starter SteveK
  • Start date Start date
S

SteveK

Shouldn't this code put the menu in the same location as my cursor?

Point screenPoint = new Point(e.X, e.Y);
Point formPoint = treeView1.PointToClient(screenPoint);



Tree_ContextMenu.Show(this, formPoint);





I'm getting it above the cursor. From whay I read on MSDN about
pointToClient it shoud do what I want... but it's not. Any ideas?





Thanks,

Steve
 
Steve,

I don't believe so. The Show method expects the Point parameter to be
in relation to the control that is passed in for the control parameter.

When you get X and Y from the MouseEventArgs instance in your event
handler, they are in the coordinates of the treeview already (assuming that
the event handler is for the tree view). Because of this, you want to omit
the call to PointToClient, and then pass screenPoint directly to the Show
method of the context menu.

Hope this helps.
 
I was trying too hard!... again! ;)

Your solution/explanation *almost* works... the menu is still above the
cursor, but only by about 30 pixels now. I suspect this might be something
else, I will read up adn look into my code to see why that is happening.

Thanks Nicholas!


Nicholas Paldino said:
Steve,

I don't believe so. The Show method expects the Point parameter to be
in relation to the control that is passed in for the control parameter.

When you get X and Y from the MouseEventArgs instance in your event
handler, they are in the coordinates of the treeview already (assuming that
the event handler is for the tree view). Because of this, you want to omit
the call to PointToClient, and then pass screenPoint directly to the Show
method of the context menu.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SteveK said:
Shouldn't this code put the menu in the same location as my cursor?

Point screenPoint = new Point(e.X, e.Y);
Point formPoint = treeView1.PointToClient(screenPoint);



Tree_ContextMenu.Show(this, formPoint);





I'm getting it above the cursor. From whay I read on MSDN about
pointToClient it shoud do what I want... but it's not. Any ideas?





Thanks,

Steve
 
Back
Top