Export (0) Print
Expand All
Expand Minimize

Control.ClientSize Property

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

[Visual Basic]
Public Property ClientSize As Size
[C#]
public Size ClientSize {get; set;}
[C++]
public: __property Size get_ClientSize();
public: __property void set_ClientSize(Size);
[JScript]
public function get ClientSize() : Size;
public function set ClientSize(Size);

Property Value

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

Remarks

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.

Example

[Visual Basic, C#, C++] The following 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.

[Visual Basic] 
Private Sub AutoSizeControl(control As Control, textPadding As Integer)
   ' Create a Graphics object for the Control.
   Dim g As Graphics = control.CreateGraphics()
   
   ' Get the Size needed to accommodate the formatted Text.
   Dim preferredSize As Size = g.MeasureString( _
     control.Text, control.Font).ToSize()
   
   ' Pad the text and resize the control.
   control.ClientSize = New Size( _
     preferredSize.Width + textPadding * 2, _
     preferredSize.Height + textPadding * 2)
   
   ' Clean up the Graphics object.
   g.Dispose()
End Sub

[C#] 
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.Text, control.Font).ToSize();

   // Pad the text and resize the control.
   control.ClientSize = new Size(
      preferredSize.Width + (textPadding * 2), 
      preferredSize.Height+(textPadding * 2) );

   // Clean up the Graphics object.
   g.Dispose();
}

[C++] 
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.
        g->Dispose();
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Control Class | Control Members | System.Windows.Forms Namespace | ClientRectangle | DrawMode

Show:
© 2014 Microsoft