Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
Control Class
Control Methods
 GetPreferredSize Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
Control..::.GetPreferredSize Method

Retrieves the size of a rectangular area into which a control can be fitted.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic
Public Overridable Function GetPreferredSize ( _
    proposedSize As Size _
) As Size
C#
public virtual Size GetPreferredSize(
    Size proposedSize
)
Visual C++
public:
virtual Size GetPreferredSize(
    Size proposedSize
)
F#
abstract GetPreferredSize : 
        proposedSize:Size -> Size 
override GetPreferredSize : 
        proposedSize:Size -> Size 

Parameters

proposedSize
Type: System.Drawing..::.Size
The custom-sized area for a control.

Return Value

Type: System.Drawing..::.Size
An ordered pair of type System.Drawing..::.Size representing the width and height of a rectangle.

The behavior of GetPreferredSize differs by control. The LayoutEngine might be unable to assign the returned size to the control. You can return a size larger than the constraints indicated in the proposedSize parameter, but proposedSize should decrease as the constraint decreases. For example, GetPreferredSize(new Size(100, 0)) should not be wider than GetPreferredSize(new Size(200, 0)). The exception is a proposedSize of 0 or Size.Empty, which are defined as unconstrained.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Explain please.      Alexander VI   |   Edit   |   Show History
The previously posted code example works, but i am not sure it is correct.
If i have many properties which affect on preferred size calculation.
Image size, Font size, Text length, Paddings and etc.
It will be multiple calls to ResetPreferedSize methods and finally multiple calls to PerformLayout() method of parent control, during setting up initial setting for the control.
This can affect on application performance if SuspendLayout method of the parent control was not called before this control initialization.

Please MS .NET Developers.
Post an official example "How to implement Auto-sizing and GetPreferredSize method?" 
Tags What's this?: Add a tag
Flag as ContentBug
Explain please.      Alexander VI   |   Edit   |   Show History
How to implement correctly all auto-sizing mechanism of the controls?
How to notify the layout system about changes of PreferredSize property correctly?
Here is a part of my code.

        // Cached preferred size.
        // I do not want recalculate the preferred size each time when the PreferredSize property is accessed or
        // the GetPreferredSize method is called.
        private Size _PreferredSize ;

       
        // I know that this method is called by PreferredSize property with proposedSize.IsEmpty
        // It also called directly by Layout engine of the parent control with specified proposedSize
        public override Size GetPreferredSize(Size proposedSize) {
            if (_PreferredSize.IsEmpty) this.UpdatePreferredSize();
            if (proposedSize.IsEmpty) return _PreferredSize;


            // This is required to allow an anchored and one direction auto-sized control to be resized.
            // If PreferredSize Width or Heigh is Zero this means the control is one direction auto-sized.
            Size ps = _PreferredSize;
            if (ps.Width == 0) {
                ps.Width = proposedSize.Width < Int32.MaxValue ? proposedSize.Width : this.Width;
            }
            if (ps.Height == 0) {
                ps.Height = proposedSize.Height < Int32.MaxValue ? proposedSize.Height : this.Height;
            }

            // this is required to allow an auto-sized GrowOnly control to be Docked with ability to Grow - be larger then PreferredSize
            if (this.AutoSizeMode == AutoSizeMode.GrowOnly &;;&;; this.Dock != DockStyle.None) {
                ps = new Size(Math.Max(ps.Width, this.Width), Math.Max(ps.Height, this.Height));
            }
            return ps;
        }
       

        // Called from UpdatePreferredSize or from any other place (for example OnPaint) to change the Cached Preferred Size.
        protected virtual void SetPreferredSize(Size value) {
            if (_PreferredSize != value) {
                _PreferredSize = value;
                 OnPreferredSizeChanged(new EventArgs());
            }
        }


        // Called when any property (Image, Text and etc.) affected on  PreferredSize  was changed.
        public void ResetPreferredSize() {
            SetPreferredSize(Size.Empty);
        }


        // Here is a place where PreferredSize is calculated
        protected virtual void UpdatePreferredSize(){
          Size ps = Size.Empty;
          .
          .
          .
          .
    
          SetPreferredSize(ps);
        }



        protected virtual void OnPreferredSizeChanged(System.EventArgs e) {
            if (PreferredSizeChanged != null) PreferredSizeChanged(this, e);

            if (this.AutoSize) {
                Control pc = this.Parent;
                if (pc == null) {
                    // What to do if there is no parent?
                    // Should i resize the control by myself (this.Size = bla bla bla)?
                    this.Size = this.PreferredSize; // Like here, but depends of AutoSizeMode of course.
                }
                else {
                    // Is this code is correct?
                    // Is there any better way to notify the layout engine about
                    // the changes and force it to resize the control?
                    pc.PerformLayout(this, "PreferredSize");
                }
            }
        }
            
        public event System.EventHandler PreferredSizeChanged;
Tags What's this?: Add a tag
Flag as ContentBug
Explain please.      LSJFDKCOAIHKFOHFEXOHAOFXK   |   Edit   |   Show History
This documentation is very confusing. If GetPreferredSize "Retrieves the size of a rectangular area into which a control can be fitted", is it just returning the minimum size necessary to display the control? What constraints is it considering when it computes the minimum size? The content of the control? Its borders? Its padding?

And what does ProposedSize mean? The sentence "you can return a size larger than the constraints indicated in the proposedSize parameter, but proposedSize should decrease as the constraint decreases" is completely baffling.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker