Results 1 to 4 of 4

Thread: [RESOLVED] [2008] Getting a Location of a Panel on a Form?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    Resolved [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?

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    Re: [2008] Getting a Location of a Panel on a Form?

    Think I found it:

    Dim pt As Point = Me.PointToScreen(Me.Panel5.Location)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    Thumbs up 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:
    1. 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:
    1. Private Sub GetDimmingPanelLocations()
    2.         intpnlDimmingController_Zone1_LocationY = Panel3.Parent.PointToScreen(Panel3.Location).Y
    3. 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
  •  



Click Here to Expand Forum to Full Width