Results 1 to 16 of 16

Thread: [2008] get a controls x/y relative to form

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    [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

  2. #2
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2008] get a controls x/y relative to form

    I don't know of a direct function but it might possibly more efficient to go PointToScreen off of the immediate control and then use those coordinates to go PointToClient with the Form.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2008] get a controls x/y relative to form

    What if you did a PointToScreen and then translated that back to a PointToClient as the PointToClient should resolve to the Forms orgin point instead. But perhaps using a combination of the two you can resolve it to the forms orgin with PointtoScreen if the PointToClient does resolve to the containers location orgin and not the Forms orgin location.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2008] get a controls x/y relative to form

    This appears to work:

    Code:
    MessageBox.Show(Me.Button1.FindForm.PointToClient(Me.Button1.PointToScreen(Me.Button1.Location)).ToString)

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2008] get a controls x/y relative to form

    Try it against a nested control. One that is in a Panel or GroupBox.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2008] get a controls x/y relative to form

    Well it does work except you need to subtract to location coords of the immediate control from the final answer as given above.

    Edit: Changed word to Control

  7. #7
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2008] get a controls x/y relative to form

    I.e.

    Code:
    MessageBox.Show((Me.Button1.FindForm.PointToClient(Me.Button1.PointToScreen(Me.Button1.Location)) - New Size(Me.Button1.Location)).ToString)

  8. #8
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2008] get a controls x/y relative to form

    I nested a Button inside a Panel, inside a Panel, inside a GroupBox and it worked as listed in my last post.

    I don't know if it's any more efficient though.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2008] get a controls x/y relative to form

    Thanks for testing it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2008] get a controls x/y relative to form

    This is a little more non-specific:

    Code:
        Private Function FindFormCoordinates(ByVal control As Control) As Point
            Return control.FindForm.PointToClient(control.PointToScreen(control.Location) - New Size(control.Location))
        End Function
    If you find a problem with it, please let me know.
    If you find a more efficient method, please let me know.
    If you find there's a framework method, please let me know.
    If you get tired of letting me know, don't let me know.

  11. #11

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] get a controls x/y relative to form

    All the suggested methods seem to work, but I am not sure if there is anything I gain by using one over mine.

    I can do some performance testing, because the code is actually used with some painting code, so speed will be somewhat important if there is a decent difference between the methods.

  12. #12

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] get a controls x/y relative to form

    Quote Originally Posted by FourBlades
    This is a little more non-specific:

    Code:
        Private Function FindFormCoordinates(ByVal control As Control) As Point
            Return control.FindForm.PointToClient(control.PointToScreen(control.Location) - New Size(control.Location))
        End Function
    If you find a problem with it, please let me know.
    If you find a more efficient method, please let me know.
    If you find there's a framework method, please let me know.
    If you get tired of letting me know, don't let me know.
    Actually, You could take this, but simplify it down to this

    (where C is the control to get coords of)
    Code:
    Return C.FindForm.PointToClient(C.Parent.PointToScreen(C.Location))

  13. #13
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2008] get a controls x/y relative to form

    I knew as soon as I did the subtraction that that implied a simplification was available...just didn't look for one since I didn't know if the whole thing was useful at all.
    Thanks for the post back.

  14. #14

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2008] get a controls x/y relative to form

    well im almost done with this little project and it has some nice results so I will post the source once im done.

  15. #15
    Member
    Join Date
    Jul 2005
    Location
    London
    Posts
    44

    Re: [2008] get a controls x/y relative to form

    Quote Originally Posted by FourBlades
    This is a little more non-specific:

    Code:
        Private Function FindFormCoordinates(ByVal control As Control) As Point
            Return control.FindForm.PointToClient(control.PointToScreen(control.Location) - New Size(control.Location))
        End Function
    If you find a problem with it, please let me know.
    If you find a more efficient method, please let me know.
    If you find there's a framework method, please let me know.
    If you get tired of letting me know, don't let me know.
    Sadly I broke it, it doesn't seem to work inside splitframes.

    Anhar

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

    Re: [2008] get a controls x/y relative to form

    I haven't read this whole thread but I didn't see the "most correct" answer posted so here goes:
    vb.net Code:
    1. Me.PointToClient(Me.MyControl.PointToScreen(Point.Empty))
    Of course, because this is VB 2008 you could create an extension method:
    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2. Imports System.Windows.Forms
    3.  
    4. Module ControlExtensions
    5.  
    6.     <Extension()> _
    7.     Public Function PointToForm(ByVal target As Control, ByVal p As Point) As Point
    8.         Return target.FindForm().PointToClient(target.PointToScreen(p))
    9.     End Function
    10.  
    11. End Module
    and then call it as though it's a member of the control:
    vb.net Code:
    1. Dim loc As Point = Me.Button1.PointToForm(Point.Empty)
    or even more specific:
    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2. Imports System.Windows.Forms
    3.  
    4. Module ControlExtensions
    5.  
    6.     <Extension()> _
    7.     Public Function GetLocationInForm(ByVal target As Control) As Point
    8.         Return target.FindForm().PointToClient(target.PointToScreen(Point.Empty))
    9.     End Function
    10.  
    11. End Module
    and then call it as though it's a member of the control:
    vb.net Code:
    1. Dim loc As Point = Me.Button1.GetLocationInForm()
    Last edited by jmcilhinney; Nov 5th, 2008 at 12:29 AM.
    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

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