Export (0) Print
Expand All
Expand Minimize

Control.ClientSize Property

Gets or sets the height and width of the client area of the control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public:
property Size ClientSize {
	Size get ();
	void set (Size value);
}
/** @property */
public Size get_ClientSize ()

/** @property */
public void set_ClientSize (Size value)

public function get ClientSize () : Size

public function set ClientSize (value : Size)

Property Value

A Size that represents the dimensions of the client area of the control.

The client area of a control is the bounds of the control, minus the nonclient elements such as scroll bars, borders, title bars, and menus. The SetClientSizeCore method is called to set the ClientSize property. The ClientSize property is not always changed through its set method so you should override the SetClientSizeCore method to ensure that your code is executed when the ClientSize property is set.

The Size.Width and Size.Height properties represent the width and height of the client area of the control. You can use this property to obtain the size of the client area of the control for tasks such as drawing on the surface of the control.

For more information about drawing on controls, see Rendering a Windows Forms Control.

The following code example resizes the specified control so the control will accommodate its formatted text. The formatted text is the Text property with the control's assigned Font applied to the text. The AutoSizeControl method in this example also has a textPadding parameter that represents the padding to apply to all edges of the control. To make the padding appear equal, align the text with the ContentAlignment.MiddleCenter value, if your control supports it.

private:
   void AutoSizeControl( Control^ control, int textPadding )
   {
      
      // Create a Graphics object for the Control.
      Graphics^ g = control->CreateGraphics();
      
      // Get the Size needed to accommodate the formatted Text.
      System::Drawing::Size preferredSize = g->MeasureString( control->Text, control->Font ).ToSize();
      
      // Pad the text and resize the control.
      control->ClientSize = System::Drawing::Size( preferredSize.Width + (textPadding * 2), preferredSize.Height + (textPadding * 2) );
      
      // Clean up the Graphics object.
      delete g;
   }

private void AutoSizeControl(Control control, int textPadding)
{
    // Create a Graphics object for the Control.
    Graphics g = control.CreateGraphics();
    // Get the Size needed to accommodate the formatted Text.
    Size preferredSize = g.MeasureString(control.get_Text(), control.
        get_Font()).ToSize();
    // Pad the text and resize the control.
    control.set_ClientSize(new Size(preferredSize.get_Width() 
        + textPadding * 2, preferredSize.get_Height() + textPadding * 2));
    // Clean up the Graphics object.
    g.Dispose();
} //AutoSizeControl

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft