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. Centering a form?!?

Centering a form?!?

Scheduled Pinned Locked Moved C#
question
7 Posts 4 Posters 1 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.
  • M Offline
    M Offline
    monrobot13
    wrote on last edited by
    #1

    I must be going mental or something because I can't for the life of me get one form to center on top of another. It seems to me that this code should work, but it doesn't.

    this.Top = (this.Owner.Height / 2) - this.Height; // I've tried both + and -
    this.Left = (this.Owner.Width / 2) + this.Width;

    but it never centers the form. What am I doing wrong here? - monrobot13

    K 1 Reply Last reply
    0
    • M monrobot13

      I must be going mental or something because I can't for the life of me get one form to center on top of another. It seems to me that this code should work, but it doesn't.

      this.Top = (this.Owner.Height / 2) - this.Height; // I've tried both + and -
      this.Left = (this.Owner.Width / 2) + this.Width;

      but it never centers the form. What am I doing wrong here? - monrobot13

      K Offline
      K Offline
      Kastro
      wrote on last edited by
      #2

      Your formula is wrong... Try this:

      this.Top = (this.Owner.Height - this.Height) / 2;
      this.Left = (this.Owner.Width - this.Width) / 2;

      I 1 Reply Last reply
      0
      • K Kastro

        Your formula is wrong... Try this:

        this.Top = (this.Owner.Height - this.Height) / 2;
        this.Left = (this.Owner.Width - this.Width) / 2;

        I Offline
        I Offline
        Ista
        wrote on last edited by
        #3

        actually you should set the Location property. the top and left are old vb'er ways plus the form property has a center option property

        N 1 Reply Last reply
        0
        • I Ista

          actually you should set the Location property. the top and left are old vb'er ways plus the form property has a center option property

          N Offline
          N Offline
          Nick Parker
          wrote on last edited by
          #4

          Ista wrote: actually you should set the Location property. the top and left are old vb'er ways It's really irrelevant, a Location property is simple defined as a Point structure that sets the Top and Left properties of the form. -Nick Parker

          I 1 Reply Last reply
          0
          • N Nick Parker

            Ista wrote: actually you should set the Location property. the top and left are old vb'er ways It's really irrelevant, a Location property is simple defined as a Point structure that sets the Top and Left properties of the form. -Nick Parker

            I Offline
            I Offline
            Ista
            wrote on last edited by
            #5

            relavancy has nothing to do with it really. I'm not an expert yet, but I play one at work. Yeah and here too.

            N 1 Reply Last reply
            0
            • I Ista

              relavancy has nothing to do with it really. I'm not an expert yet, but I play one at work. Yeah and here too.

              N Offline
              N Offline
              Nick Parker
              wrote on last edited by
              #6

              Ista wrote: relavancy has nothing to do with it really :rolleyes: -Nick Parker

              I 1 Reply Last reply
              0
              • N Nick Parker

                Ista wrote: relavancy has nothing to do with it really :rolleyes: -Nick Parker

                I Offline
                I Offline
                Ista
                wrote on last edited by
                #7

                lol:laugh: I'm not an expert yet, but I play one at work. Yeah and here too.

                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