|
-
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
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
|