Export (0) Print
Expand All

ListBox.PreferredHeight Property

Gets the combined height of all items in the ListBox.

[Visual Basic]
Public ReadOnly Property PreferredHeight As Integer
[C#]
public int PreferredHeight {get;}
[C++]
public: __property int get_PreferredHeight();
[JScript]
public function get PreferredHeight() : int;

Property Value

The combined height, in pixels, of all items in the control.

Remarks

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 container it is located within.

Example

[Visual Basic, C#, C++] The following 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 assumes that a ListBox control, named listBox1, has been added to a form.

[Visual Basic] 
Private Sub SizeMyListBox()
   ' Add items to the ListBox.
   Dim x As Integer
   For x = 0 To 19
      listBox1.Items.Add(("Item " + x.ToString()))
   Next x
   ' Set the height of the ListBox to the preferred height to display all items.
   listBox1.Height = listBox1.PreferredHeight
End Sub 'SizeMyListBox

[C#] 
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;
}

[C++] 
private:
   void SizeMyListBox()
   {
      // Add items to the ListBox.
      for(int x = 0; x < 20; x++)
      {
         listBox1->Items->Add(String::Format( S"Item {0}", __box(x)));
      }
      // Set the height of the ListBox to the preferred height to display all items.
      listBox1->Height = listBox1->PreferredHeight;
   }

[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

See Also

ListBox Class | ListBox Members | System.Windows.Forms Namespace | Height

Show:
© 2014 Microsoft