Export (0) Print
Expand All

ListBox::PreferredHeight Property

Gets the combined height of all items in the ListBox.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[BrowsableAttribute(false)]
public:
property int PreferredHeight {
	int get ();
}

Property Value

Type: System::Int32
The combined height, in pixels, of all items in the control.

This property enables you to determine the height that the ListBox needs to be sized to, in order to display every available item in the list and to avoid displaying vertical scroll bars. If the amount of items in the ListBox is large, sizing the control using the value of the PreferredHeight property might cause the ListBox to be sized outside of the client area of the form or the ListBox container.

The following code example demonstrates how to set the Size property of a ListBox based on the value of the PreferredHeight property in order to display all items in the ListBox without use of scroll bars. This example requires that a ListBox control, named listBox1, has been added to a form.

private:
   void SizeMyListBox()
   {
      // Add items to the ListBox. 
      for ( int x = 0; x < 20; x++ )
      {
         listBox1->Items->Add( String::Format( "Item {0}", x ) );
      }
      listBox1->Height = listBox1->PreferredHeight;
   }

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft