|
-
Mar 26th, 2008, 04:27 PM
#1
[2008] get a controls x/y relative to form
The .NET framework library is a big place. I am just wondering if anyone ever came across something in WinForms, that would allow you to get the x/y of a control relative to the form, regardless of how many containers are stacked between the form and the control.
As you may or may not know, when you check something like a controls bounds, or top/left properties, they are relative to the controls container, so if a control is in a groupbox, which is in a panel, which is on a form, then there is no property of the control itself (that I noticed anyway) to give you its x/y relative to the form.
That being said, I did already resolve this with the following code.
It just loops from the control to the form, counting up all the containers x/y along the way. I am just wondering if anyone ever came across a better way.
Code:
Dim controlToLoop As Control = Button1
Dim parentForm As Form = controlToLoop.FindForm
Dim finalLocation As New Point(0, 0)
If parentForm Is Nothing Then Return
Do
With finalLocation
.X += controlToLoop.Left
.Y += controlToLoop.Top
End With
controlToLoop = controlToLoop.Parent
Loop While controlToLoop IsNot parentForm
-
Mar 26th, 2008, 05:02 PM
#2
Frenzied Member
Re: [2008] get a controls x/y relative to form
I don't know of a direct function but it might possibly more efficient to go PointToScreen off of the immediate control and then use those coordinates to go PointToClient with the Form.
-
Mar 26th, 2008, 05:04 PM
#3
Re: [2008] get a controls x/y relative to form
What if you did a PointToScreen and then translated that back to a PointToClient as the PointToClient should resolve to the Forms orgin point instead. But perhaps using a combination of the two you can resolve it to the forms orgin with PointtoScreen if the PointToClient does resolve to the containers location orgin and not the Forms orgin location.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 26th, 2008, 05:12 PM
#4
Frenzied Member
Re: [2008] get a controls x/y relative to form
This appears to work:
Code:
MessageBox.Show(Me.Button1.FindForm.PointToClient(Me.Button1.PointToScreen(Me.Button1.Location)).ToString)
-
Mar 26th, 2008, 05:15 PM
#5
Re: [2008] get a controls x/y relative to form
Try it against a nested control. One that is in a Panel or GroupBox.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 26th, 2008, 05:18 PM
#6
Frenzied Member
Re: [2008] get a controls x/y relative to form
Well it does work except you need to subtract to location coords of the immediate control from the final answer as given above.
Edit: Changed word to Control
-
Mar 26th, 2008, 05:22 PM
#7
Frenzied Member
Re: [2008] get a controls x/y relative to form
I.e.
Code:
MessageBox.Show((Me.Button1.FindForm.PointToClient(Me.Button1.PointToScreen(Me.Button1.Location)) - New Size(Me.Button1.Location)).ToString)
-
Mar 26th, 2008, 05:28 PM
#8
Frenzied Member
Re: [2008] get a controls x/y relative to form
I nested a Button inside a Panel, inside a Panel, inside a GroupBox and it worked as listed in my last post.
I don't know if it's any more efficient though.
-
Mar 26th, 2008, 05:41 PM
#9
Re: [2008] get a controls x/y relative to form
Thanks for testing it.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Mar 26th, 2008, 05:53 PM
#10
Frenzied Member
Re: [2008] get a controls x/y relative to form
This is a little more non-specific:
Code:
Private Function FindFormCoordinates(ByVal control As Control) As Point
Return control.FindForm.PointToClient(control.PointToScreen(control.Location) - New Size(control.Location))
End Function
If you find a problem with it, please let me know.
If you find a more efficient method, please let me know.
If you find there's a framework method, please let me know.
If you get tired of letting me know, don't let me know.
-
Mar 27th, 2008, 08:23 AM
#11
Re: [2008] get a controls x/y relative to form
All the suggested methods seem to work, but I am not sure if there is anything I gain by using one over mine.
I can do some performance testing, because the code is actually used with some painting code, so speed will be somewhat important if there is a decent difference between the methods.
-
Mar 27th, 2008, 08:41 AM
#12
Re: [2008] get a controls x/y relative to form
 Originally Posted by FourBlades
This is a little more non-specific:
Code:
Private Function FindFormCoordinates(ByVal control As Control) As Point
Return control.FindForm.PointToClient(control.PointToScreen(control.Location) - New Size(control.Location))
End Function
If you find a problem with it, please let me know.
If you find a more efficient method, please let me know.
If you find there's a framework method, please let me know.
If you get tired of letting me know, don't let me know. 
Actually, You could take this, but simplify it down to this
(where C is the control to get coords of)
Code:
Return C.FindForm.PointToClient(C.Parent.PointToScreen(C.Location))
-
Mar 27th, 2008, 01:59 PM
#13
Frenzied Member
Re: [2008] get a controls x/y relative to form
I knew as soon as I did the subtraction that that implied a simplification was available...just didn't look for one since I didn't know if the whole thing was useful at all.
Thanks for the post back.
-
Mar 27th, 2008, 02:02 PM
#14
Re: [2008] get a controls x/y relative to form
well im almost done with this little project and it has some nice results so I will post the source once im done.
-
Nov 4th, 2008, 10:12 AM
#15
Member
Re: [2008] get a controls x/y relative to form
 Originally Posted by FourBlades
This is a little more non-specific:
Code:
Private Function FindFormCoordinates(ByVal control As Control) As Point
Return control.FindForm.PointToClient(control.PointToScreen(control.Location) - New Size(control.Location))
End Function
If you find a problem with it, please let me know.
If you find a more efficient method, please let me know.
If you find there's a framework method, please let me know.
If you get tired of letting me know, don't let me know. 
Sadly I broke it, it doesn't seem to work inside splitframes.
Anhar
-
Nov 5th, 2008, 12:16 AM
#16
Re: [2008] get a controls x/y relative to form
I haven't read this whole thread but I didn't see the "most correct" answer posted so here goes:
vb.net Code:
Me.PointToClient(Me.MyControl.PointToScreen(Point.Empty))
Of course, because this is VB 2008 you could create an extension method:
vb.net Code:
Imports System.Runtime.CompilerServices
Imports System.Windows.Forms
Module ControlExtensions
<Extension()> _
Public Function PointToForm(ByVal target As Control, ByVal p As Point) As Point
Return target.FindForm().PointToClient(target.PointToScreen(p))
End Function
End Module
and then call it as though it's a member of the control:
vb.net Code:
Dim loc As Point = Me.Button1.PointToForm(Point.Empty)
or even more specific:
vb.net Code:
Imports System.Runtime.CompilerServices
Imports System.Windows.Forms
Module ControlExtensions
<Extension()> _
Public Function GetLocationInForm(ByVal target As Control) As Point
Return target.FindForm().PointToClient(target.PointToScreen(Point.Empty))
End Function
End Module
and then call it as though it's a member of the control:
vb.net Code:
Dim loc As Point = Me.Button1.GetLocationInForm()
Last edited by jmcilhinney; Nov 5th, 2008 at 12:29 AM.
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
|