0

This seems really simple but really having an issue! I have a button, that when I click on it, I want the x,y coordinates of one corner so I can pop up a window in a certain location from the button. I know the height and width of the button, and I can get the coordinates of where I have clicked with the mouse, but really struggling to get ccordinates of a corner. This is what I have so far:

Private Sub HandleClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim clickLocationPos = Mouse.GetPosition(Window.GetWindow(Me))

        Dim xPos = clickLocationPos.X
        Dim yPos = clickLocationPos.Y
    End If
End Sub
Navvy
  • 237
  • 3
  • 9
  • 23
  • Which corner are you looking for? – the_lotus Jan 07 '19 at 14:19
  • 1
    sender is the button. Then https://stackoverflow.com/questions/386731/get-absolute-position-of-element-within-the-window-in-wpf –  Jan 07 '19 at 14:20
  • 1
    So your actual question isn't how to get the coordinates. It's how to display a popup in relation to a button. Something already available eg through the [Popup](https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/popup-overview) or [ToolTip](https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/tooltip-overview) elements. – Panagiotis Kanavos Jan 07 '19 at 14:21
  • @PanagiotisKanavos no. I can get my popup, it's just not in the position I want, I have everything else working but the position I want it in – Navvy Jan 07 '19 at 14:23
  • @the_lotus any corner will do! – Navvy Jan 07 '19 at 14:24
  • 1
    @Navvy you are trying to copy Winforms techniques to WPF. You shouldn't even be trying to capture mouse events like this. Who says the popup has to appear in response to a *mouse click*? WPF works with commands. Whou says your *code* needs to create and display the popup window at all? This could be an element of the target that is shown or hidden as appropriate. WIndows Forms doesn't calculate coordinates either, it displays windows in relation to each other. – Panagiotis Kanavos Jan 07 '19 at 14:29
  • @Navvy even in Windows Forms, the *Click* event doesn't have coordinates. Clicks aren't raised by mice, they are raised by any behaviour that's considered a click including key presses. That's why people either retrieve the control's location from the control itself. – Panagiotis Kanavos Jan 07 '19 at 14:32
  • @Will thank you - you've pointed me in the correct direction! – Navvy Jan 07 '19 at 14:38

1 Answers1

0

This should give the clicked Button's top-left coordinate relative to the parent window:

Private Sub HandleClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim button = CType(sender, Button)
    Dim topLeftCorner = button.TransformToAncestor(Me).Transform(New Point(0, 0))
    Dim xPos = topLeftCorner.X
    Dim yPos = topLeftCorner.Y
End Sub
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Or `[Point] = button.TranslatePoint(New Point(0, 0), Me)` or (WinForms style) `[Point] = Me.PointFromScreen(button.PointToScreen(New Point(0, 0)))` – Jimi Jan 07 '19 at 14:52