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)

public int PreferredHeight { get; }
/** @property */
public int get_PreferredHeight ()

public function get PreferredHeight () : int

Not applicable.

Property Value

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("Item " + x.ToString());
   }
   // Set the height of the ListBox to the preferred height to display all items.
   listBox1.Height = listBox1.PreferredHeight;
}

private void SizeMyListBox()
{
    // Add items to the ListBox.
    for (int x = 0; x < 20; x++) {
        listBox1.get_Items().Add("Item " + Convert.ToString(x));
    }
    // Set the height of the ListBox to the preferred height 
    // to display all items.
    listBox1.set_Height(listBox1.get_PreferredHeight());
} //SizeMyListBox

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft