|
-
Apr 22nd, 2008, 11:42 PM
#1
Thread Starter
Addicted Member
[RESOLVED] [2008] Getting a Location of a Panel on a Form?
Yo!
I have 3 panels (let's call them Panel1, Panel2 and Panel3). They are stacked so the first panel is the parent of the second panel and the second panel is the parent of the third panel.
I need to establish the Location.Y value for the 3rd panel, in relation to the screen position 0,0. Because the 3rd panel is inside the 2nd panel, the Location.Y value is relative to the 2nd panel and so on.
I am trying to loop through the parents and just add their Location.Y values together to give me the screen-relative Location.Y value of Panel3 (without luck thus far).
Is there a method for getting a Location.Y relative to the screen rather than the parent?
-
Apr 22nd, 2008, 11:48 PM
#2
Thread Starter
Addicted Member
Re: [2008] Getting a Location of a Panel on a Form?
Think I found it:
Dim pt As Point = Me.PointToScreen(Me.Panel5.Location)
-
Apr 22nd, 2008, 11:56 PM
#3
Re: [2008] Getting a Location of a Panel on a Form?
That's actually not going to work. You would need to do this:
vb.net Code:
Dim pt As Point = Me.Panel5.Parent.PointToScreen(Me.Panel5.Location)
Here's why. Imagine that you have the form on the screen at location (100,100). Imagine that you have Panel1 on the form at Location (200,200). Imagine that you have Panel2 on Panel1 at location (10,10). Now, by your code the form will take the inner Panel's Location, which is (10,10), and convert that to screen coordinates to get (110,110). That's obviously not correct. You need to tell the inner Panel's parent control, which is the outer Panel, to calculate the screen coordinates. The code I provided will always work no matter how deeply the controls are nested.
-
Apr 23rd, 2008, 06:08 PM
#4
Thread Starter
Addicted Member
Re: [2008] Getting a Location of a Panel on a Form?
Hey JM. You are correct once again, thanks for the help. Can I donate to you via Paypal?!
Code:
Dim pt As Point = Me.Panel5.Parent.PointToScreen(Me.Panel5.Location)
On a serious note, that code works perfectly on the proviso that the panel is visible at the time you grab the values. I had it working perfectly in my test application...and as soon as I put it into the real application (which includes many panels, all starting visible=false except for home, and then scrolls through the panels by changing the visibility status) which made the intial code give me a location which was way out. So I've discovered that the panel visible=true to get its position. It makes sense, even though the location of the panel is still defined regardless of its visibility?
For the moment I've added a sub that is called when the visibilitychanged event occurs which works a treat...
Code:
Private Sub GetDimmingPanelLocations()
intpnlDimmingController_Zone1_LocationY = Panel3.Parent.PointToScreen(Panel3.Location).Y
End Sub
Thanks for the help
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
|