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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. ClientRectangle in VS 2005

ClientRectangle in VS 2005

Scheduled Pinned Locked Moved C#
csharpvisual-studiowinformsquestion
3 Posts 2 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
    MBursill
    wrote on last edited by
    #1

    In WinForms 1.1 if you add a menu to a form and then grab the ClientRectangle property you get the Rectangle of the visible client area minus the menu. In WinForms 2.0 if you add a MenuStrip to a form and then grab the ClientRectangle property you get the Rectangle for the area including the Menu. Is there a replacement property to give me the same behaviour as the old WinForms? This should be simple but I'm just not finding it. Is it necessary to write code that iterates though each control on the form, checks the dock property and generates a new property (possibly called VisibleClientRectangle)? Thanks. -Mike.

    M I 2 Replies Last reply
    0
    • M MBursill

      In WinForms 1.1 if you add a menu to a form and then grab the ClientRectangle property you get the Rectangle of the visible client area minus the menu. In WinForms 2.0 if you add a MenuStrip to a form and then grab the ClientRectangle property you get the Rectangle for the area including the Menu. Is there a replacement property to give me the same behaviour as the old WinForms? This should be simple but I'm just not finding it. Is it necessary to write code that iterates though each control on the form, checks the dock property and generates a new property (possibly called VisibleClientRectangle)? Thanks. -Mike.

      M Offline
      M Offline
      MBursill
      wrote on last edited by
      #2

      After doing some research (and using Reflector to poke around in the ClientRectangle property) I can see that there is no simple way to do what I want. I came up with a solution:

      /// Return the client area of the form after subtracting docked controls
      public new Rectangle ClientRectangle
      {
      get
      {
      Rectangle _clientRectangle = base.ClientRectangle;

          foreach (Control currentControl in Controls)
          {
              switch (currentControl.Dock)
              {
                  case DockStyle.Top:
                      \_clientRectangle.Height -= currentControl.Height;
                      \_clientRectangle.Offset(0, currentControl.Height);
                      break;
      
                  case DockStyle.Bottom:
                      \_clientRectangle.Height -= currentControl.Height;
                      break;
      
                  case DockStyle.Left:
                      \_clientRectangle.Width -= currentControl.Width;
                      \_clientRectangle.Offset(currentControl.Width, 0);
                      break;
      
                  case DockStyle.Right:
                      \_clientRectangle.Width -= currentControl.Width;
                      break;
              }
          }
      
          return \_clientRectangle;
      }
      

      }

      However, iterating over every control in the forms Contols collection every time you want to grab ClientRectangle is slow. Anyone have any tips on optimizing this? -Mike.

      1 Reply Last reply
      0
      • M MBursill

        In WinForms 1.1 if you add a menu to a form and then grab the ClientRectangle property you get the Rectangle of the visible client area minus the menu. In WinForms 2.0 if you add a MenuStrip to a form and then grab the ClientRectangle property you get the Rectangle for the area including the Menu. Is there a replacement property to give me the same behaviour as the old WinForms? This should be simple but I'm just not finding it. Is it necessary to write code that iterates though each control on the form, checks the dock property and generates a new property (possibly called VisibleClientRectangle)? Thanks. -Mike.

        I Offline
        I Offline
        Insincere Dave
        wrote on last edited by
        #3

        I suppose you could add a panel, set its dock to fill and use its ClientRectangle. You would need to make sure the panel is infront of any other docked controls.

        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