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. C#
  4. How to find the location of a control relative to it's containing form?

How to find the location of a control relative to it's containing form?

Scheduled Pinned Locked Moved C#
helptutorialquestion
5 Posts 2 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.
  • F Offline
    F Offline
    Furty
    wrote on last edited by
    #1

    I'm developing a control derived from the ListView control and need it to find it's position relative to the Form it's contained on. In my test Form I have the ListView positioned on a TabPage, and all the normal positioning references (this.Bounds, this.Location, this.Top, this.Left, this.ClientRectangle etc) return 0 - which is it's correct position within the TabPage, but obviously has nothing to do with it's position on the parent Form. Note that I do not need a reference to it's Screen location, just the X and Y coordinates to it's upper-left corner within the parent form - can anyone help?

    P 1 Reply Last reply
    0
    • F Furty

      I'm developing a control derived from the ListView control and need it to find it's position relative to the Form it's contained on. In my test Form I have the ListView positioned on a TabPage, and all the normal positioning references (this.Bounds, this.Location, this.Top, this.Left, this.ClientRectangle etc) return 0 - which is it's correct position within the TabPage, but obviously has nothing to do with it's position on the parent Form. Note that I do not need a reference to it's Screen location, just the X and Y coordinates to it's upper-left corner within the parent form - can anyone help?

      P Offline
      P Offline
      Philip Fitzsimons
      wrote on last edited by
      #2

      you need to offset the location by adding the parent location. int x = Left; for (Control thisParent = parent; thisParent != null; thisParent = thisParent.parent)    x += thisParent.Left;


      "When the only tool you have is a hammer, a sore thumb you will have."

      F 1 Reply Last reply
      0
      • P Philip Fitzsimons

        you need to offset the location by adding the parent location. int x = Left; for (Control thisParent = parent; thisParent != null; thisParent = thisParent.parent)    x += thisParent.Left;


        "When the only tool you have is a hammer, a sore thumb you will have."

        F Offline
        F Offline
        Furty
        wrote on last edited by
        #3

        Many thanks for the quick reply! I tried implementing your suggestion as follows: int x = Left; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) x += thisParent.Left; int y = Top; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) y += thisParent.Top; No go though I'm afraid - what that ends up giving me is a screen reference, not a refence relative to the containing form. Perhaps it has something to do with the fact the the test app is MDI? Even if so, I'd like to make a control that will work reliably in both SDI and MDI apps.

        P 1 Reply Last reply
        0
        • F Furty

          Many thanks for the quick reply! I tried implementing your suggestion as follows: int x = Left; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) x += thisParent.Left; int y = Top; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) y += thisParent.Top; No go though I'm afraid - what that ends up giving me is a screen reference, not a refence relative to the containing form. Perhaps it has something to do with the fact the the test app is MDI? Even if so, I'd like to make a control that will work reliably in both SDI and MDI apps.

          P Offline
          P Offline
          Philip Fitzsimons
          wrote on last edited by
          #4

          just stop one level earlier int x = Left; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) {    if (this.Parent != null)       x += thisParent.Left; }


          "When the only tool you have is a hammer, a sore thumb you will have."

          F 1 Reply Last reply
          0
          • P Philip Fitzsimons

            just stop one level earlier int x = Left; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) {    if (this.Parent != null)       x += thisParent.Left; }


            "When the only tool you have is a hammer, a sore thumb you will have."

            F Offline
            F Offline
            Furty
            wrote on last edited by
            #5

            Thanks for the tips Philip - after stopping one level before the MDI parent I still had a problem where the MDI child form was reporting negative location values. The final code I came up with looks like this: int x = Left; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) { if (thisParent.Parent != null && thisParent.Left > 0) x += thisParent.Left; } int y = Top; for (Control thisParent = Parent; thisParent != null; thisParent = thisParent.Parent) { if (thisParent.Parent != null && thisParent.Top > 0) y += thisParent.Top; }

            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