Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How do you calc the absolute location of a control?

How do you calc the absolute location of a control?

Scheduled Pinned Locked Moved Visual Basic
questioncsharp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    watagal
    wrote on last edited by
    #1

    Greetings! If I have a control in a split-panel, which is in a tab control, which is in a form -- how can I obtain the absolute location of the original (button) control? Dim o As Control = LogPhotosToDbBtn Dim x As Integer = LogPhotosToDbBtn.Location.X Dim y As Integer = LogPhotosToDbBtn.Location.Y While (o.Parent.Name <> "") o = o.Parent x += o.Location.X y += o.Location.Y End While Dim p1 As Point p1.X = x p1.Y = y LogFotosForm.Show() LogFotosForm.Location = p1
    X and Y are never incremented because o.Parent.Name always return "". Is there a better way (one that actually works) of trying to do this? Thanks, Karen Nooobie to OOP and VB.Net 2005

    R W 2 Replies Last reply
    0
    • W watagal

      Greetings! If I have a control in a split-panel, which is in a tab control, which is in a form -- how can I obtain the absolute location of the original (button) control? Dim o As Control = LogPhotosToDbBtn Dim x As Integer = LogPhotosToDbBtn.Location.X Dim y As Integer = LogPhotosToDbBtn.Location.Y While (o.Parent.Name <> "") o = o.Parent x += o.Location.X y += o.Location.Y End While Dim p1 As Point p1.X = x p1.Y = y LogFotosForm.Show() LogFotosForm.Location = p1
      X and Y are never incremented because o.Parent.Name always return "". Is there a better way (one that actually works) of trying to do this? Thanks, Karen Nooobie to OOP and VB.Net 2005

      R Offline
      R Offline
      rwestgraham
      wrote on last edited by
      #2

      Have you tried stepping through this in debug mode? The line: o = o.Parent will fail when you get to the form level because the form has no parent object. That may be part of your problem ...

      W 1 Reply Last reply
      0
      • R rwestgraham

        Have you tried stepping through this in debug mode? The line: o = o.Parent will fail when you get to the form level because the form has no parent object. That may be part of your problem ...

        W Offline
        W Offline
        watagal
        wrote on last edited by
        #3

        I did try to debug it, it never enters the while loop -- even tho the control is buried in several levels of parent containers. Does o.Parent.Name return the parent's container's Name property? If not, what does? Thanks, Karen Nooobie to OOP and VB.Net 2005

        R 1 Reply Last reply
        0
        • W watagal

          I did try to debug it, it never enters the while loop -- even tho the control is buried in several levels of parent containers. Does o.Parent.Name return the parent's container's Name property? If not, what does? Thanks, Karen Nooobie to OOP and VB.Net 2005

          R Offline
          R Offline
          rwestgraham
          wrote on last edited by
          #4

          watagal wrote: Does o.Parent.Name return the parent's container's Name property? It should. This works in Net 2002/2003.

          1 Reply Last reply
          0
          • W watagal

            Greetings! If I have a control in a split-panel, which is in a tab control, which is in a form -- how can I obtain the absolute location of the original (button) control? Dim o As Control = LogPhotosToDbBtn Dim x As Integer = LogPhotosToDbBtn.Location.X Dim y As Integer = LogPhotosToDbBtn.Location.Y While (o.Parent.Name <> "") o = o.Parent x += o.Location.X y += o.Location.Y End While Dim p1 As Point p1.X = x p1.Y = y LogFotosForm.Show() LogFotosForm.Location = p1
            X and Y are never incremented because o.Parent.Name always return "". Is there a better way (one that actually works) of trying to do this? Thanks, Karen Nooobie to OOP and VB.Net 2005

            W Offline
            W Offline
            watagal
            wrote on last edited by
            #5

            watagal wrote: Greetings! If I have a control in a split-panel, which is in a tab control, which is in a form -- how can I obtain the absolute location of the original (button) control? See non-working code in original post X and Y are never incremented because o.Parent.Name always return "". Is there a better way (one that actually works) of trying to do this? Ok, figured out what was happening: The first parent above my button was a split panel, while the actual split panel returns the correct Name -- the Panel1 container within the split panel container does not return a Name (I guess because you can not assign it a name). Therefore, my while loop was never entered. Here's code that does work: Dim o As Control = uxLogPhotosToDbBtn Dim x As Integer = 0 Dim y As Integer = 0 While (Not o Is Nothing) x += o.Location.X y += o.Location.Y o = o.Parent End While Dim p1 As Point p1.X = x p1.Y = y uxcLogFotosForm.Show() uxcLogFotosForm.Location = p1 Thanks again for all the help, Thanks, Karen Nooobie to OOP and VB.Net 2005

            W 1 Reply Last reply
            0
            • W watagal

              watagal wrote: Greetings! If I have a control in a split-panel, which is in a tab control, which is in a form -- how can I obtain the absolute location of the original (button) control? See non-working code in original post X and Y are never incremented because o.Parent.Name always return "". Is there a better way (one that actually works) of trying to do this? Ok, figured out what was happening: The first parent above my button was a split panel, while the actual split panel returns the correct Name -- the Panel1 container within the split panel container does not return a Name (I guess because you can not assign it a name). Therefore, my while loop was never entered. Here's code that does work: Dim o As Control = uxLogPhotosToDbBtn Dim x As Integer = 0 Dim y As Integer = 0 While (Not o Is Nothing) x += o.Location.X y += o.Location.Y o = o.Parent End While Dim p1 As Point p1.X = x p1.Y = y uxcLogFotosForm.Show() uxcLogFotosForm.Location = p1 Thanks again for all the help, Thanks, Karen Nooobie to OOP and VB.Net 2005

              W Offline
              W Offline
              watagal
              wrote on last edited by
              #6

              Now that I have accomplished what I wanted to do, Is there a faster better easier way to do this somewhat common task? Thanks, Karen Nooobie to OOP and VB.Net 2005

              M 1 Reply Last reply
              0
              • W watagal

                Now that I have accomplished what I wanted to do, Is there a faster better easier way to do this somewhat common task? Thanks, Karen Nooobie to OOP and VB.Net 2005

                M Offline
                M Offline
                Marc 0
                wrote on last edited by
                #7

                Here you go, i'll be happy to give some explanation if you want. :-D

                Imports System.Runtime.InteropServices

                Public NotInheritable Class Api

                <StructLayout(LayoutKind.Explicit)> \_
                Private Structure RECT
                    <FieldOffset(0)> Public left As Integer
                    <FieldOffset(4)> Public top As Integer
                    <FieldOffset(8)> Public right As Integer
                    <FieldOffset(12)> Public bottom As Integer
                End Structure
                
                <DllImport("User32.dll", CharSet:=System.Runtime.InteropServices.CharSet.Auto)> \_
                Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
                End Function
                
                ''' <summary>Get the location of a control in screen coordinates.</summary>
                ''' <param name="control">The control who's location will be returned.</param>
                ''' <returns>Returns the location of the control in screen coordinates.</returns>
                Public Shared Function AbsoluteLocation(ByVal control As Control) As Point
                    If (control Is Nothing) Then Throw New ArgumentNullException("control")
                
                    Dim rect As New rect
                    GetWindowRect(control.Handle, rect)
                    Return New Point(rect.left, rect.top)
                End Function
                

                End Class

                Pompiedompiedom... ;)


                "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups