By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,699 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,699 IT Pros & Developers. It's quick & easy.

Finding coordinates of an item in a panel on form

P: n/a
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!
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies

P: n/a
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.

Brian Henry wrote:
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!

Nov 21 '05 #2

P: n/a
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

"Matt" <ba*******@hotmail.com> wrote in message
news:eXMDe.13023$N91.4871@trnddc08...
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.

Brian Henry wrote:
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!

Nov 21 '05 #3

P: n/a

You are after the PointToControl and PointToScreen methods of Control,
I think.

Brian Henry wrote:
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

"Matt" <ba*******@hotmail.com> wrote in message
news:eXMDe.13023$N91.4871@trnddc08...
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.

Brian Henry wrote:
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!


Nov 21 '05 #4

P: n/a
"Brian Henry" <no****@nospam.microsoft.com> wrote in message news:OR**************@TK2MSFTNGP14.phx.gbl...
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

"Matt" <ba*******@hotmail.com> wrote in message
news:eXMDe.13023$N91.4871@trnddc08...
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.

Brian Henry wrote:
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!



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.

--
Al Reid
Nov 21 '05 #5

P: n/a
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.

"Al Reid" <ar*****@reidDASHhome.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
"Brian Henry" <no****@nospam.microsoft.com> wrote in message
news:OR**************@TK2MSFTNGP14.phx.gbl...
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

"Matt" <ba*******@hotmail.com> wrote in message
news:eXMDe.13023$N91.4871@trnddc08...
> 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.
>
>
>
> Brian Henry wrote:
>
>>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!
>>
>>



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.

--
Al Reid

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.