Finding coordinates of an item in a panel on form

  • Thread starter Thread starter Brian Henry
  • Start date Start date
B

Brian Henry

Say I put an item into a panel and place it onto a form, and I want to know
where on the form the item is in X,Y positions... but If I check the
location property of the item in the panel, that is relative to the X,Y
cordinates (0,0) of the panel, not of the form. How would I know where the
item is on the entire form even though it is in a panel?

Like if the panel is 10 from left and 10 from top of the form, and I place a
textbox in the panel at 10 from its left and 10 from its top, the textbox's
location is listed as (10,10) even though it is (20,20) relative to the
form.

How would I get that 20,20 location instead of the one relative to its
parent container? thanks!
 
The values of the locations would be (assuming panel1 is the name of
your panel, item1 is the name of the object, and form1 is the name of
the form):

x = item1.location.x + panel1.location.x
y = item1.location.y + panel1.location.y

In VB.Net 05, there are some nifty functions that do this, but I haven't
looked into them yet.
 
well yes, I already knew you could add them together... but it gets complex
if you have panels in panels in panels... this is being used in a custom
control, so it would be hard to know what it's exactly in.. so a function or
routine that could figure it out automatically would be nice
 
Brian Henry said:
well yes, I already knew you could add them together... but it gets complex
if you have panels in panels in panels... this is being used in a custom
control, so it would be hard to know what it's exactly in.. so a function or
routine that could figure it out automatically would be nice

Would something like this work?

///
Public Function PointOnForm(ByVal ctl As Control) As Point
Dim o As Object
Dim p as New Point

o = ctl
Do While Not TypeOf o Is Form
p.X += o.location.x
p.Y += o.location.y
o = o.Parent
Loop

Return p

End Function
\\\

It will need some modifications if you use "Option Strict = On", however.
 
yeah, I was already doing it like this

Private Function FindTopLocation(ByVal item As Control) As Integer

Dim i_top As Integer = 0

i_top = item.Top

If Not TypeOf item Is Form Then

i_top += FindTopLocation(item.Parent)

End If

Return i_top

End Function

Private Function FindLeftLocation(ByVal item As Control) As Integer

Dim i_left As Integer = 0

i_left = item.Left

If Not TypeOf item Is Form Then

i_left += FindLeftLocation(item.Parent)

End If

Return i_left

End Function



was just hoping there is a function that did it automatically.
 
Back
Top