-
Aug 14th, 2014, 12:35 PM
#1
Thread Starter
Banned
[RESOLVED] ContextMenuStrip opening on wrong location O.o
I clicked on the "Facebook" button but it shows the context menu on the wrong place. Code I used:
Code:
Private Sub BookmarkItem_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Right Then
ContextMenuStrip1.Show(e.Location)
clickedbutton = CType(sender, ButtonItem)
End If
End Sub
Fixed:
vb.net Code:
ContextMenuStrip1.Show(New Point(MousePosition))
Last edited by berny22; Aug 14th, 2014 at 12:40 PM.
-
Aug 14th, 2014, 09:56 PM
#2
Re: [RESOLVED] ContextMenuStrip opening on wrong location O.o
By way of explanation, the ContextMenuStrip.Show method takes a Point in screen coordinates as an argument, which is why the parameter is named "screenLocation". The e.Location property in a MouseUp event handler is relative to the control raising the event, not the screen. As an alternative to the solution shown, you could do this:
Code:
Dim screenLocation = DirectCast(sender, Control).PointToScreen(e.Location)
ContextMenuStrip1.Show(screenLocation)
I'm not suggesting that that is better, just that it demonstrates the conversion from the controls client coordinates to screen coordinates.
-
Aug 15th, 2014, 08:21 AM
#3
Thread Starter
Banned
Re: [RESOLVED] ContextMenuStrip opening on wrong location O.o
Originally Posted by jmcilhinney
By way of explanation, the ContextMenuStrip.Show method takes a Point in screen coordinates as an argument, which is why the parameter is named "screenLocation". The e.Location property in a MouseUp event handler is relative to the control raising the event, not the screen. As an alternative to the solution shown, you could do this:
Code:
Dim screenLocation = DirectCast(sender, Control).PointToScreen(e.Location)
ContextMenuStrip1.Show(screenLocation)
I'm not suggesting that that is better, just that it demonstrates the conversion from the controls client coordinates to screen coordinates.
Or to make it shorter?
vb.net Code:
ContextMenuStrip1.Show(DirectCast(sender, Control).PointToScreen(e.Location))
-
Aug 15th, 2014, 11:01 AM
#4
Re: [RESOLVED] ContextMenuStrip opening on wrong location O.o
Originally Posted by berny22
Or to make it shorter?
vb.net Code:
ContextMenuStrip1.Show(DirectCast(sender, Control).PointToScreen(e.Location))
Absolutely, but your original code is shorter still, so you'd probably go with that. I just provided that expanded example to show more clearly why the original issue arose and the change solved it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|