ListBox.PreferredHeight 属性

定义

获取 ListBox 中所有项的组合高度。Gets the combined height of all items in the ListBox.

public:
 property int PreferredHeight { int get(); };
[System.ComponentModel.Browsable(false)]
public int PreferredHeight { get; }
member this.PreferredHeight : int
Public ReadOnly Property PreferredHeight As Integer

属性值

控件中所有项的组合高度(以像素为单位)。The combined height, in pixels, of all items in the control.

属性

示例

下面的代码示例演示如何根据 PreferredHeight 属性的值设置 ListBoxSize 属性,以便在不使用滚动条的情况下显示 ListBox 中的所有项。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. 此示例要求已将名为 listBox1ListBox 控件添加到窗体中。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;
   }
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 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

注解

此属性使您可以确定需要调整 ListBox 大小的高度,以便在列表中显示每个可用项并避免显示垂直滚动条。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. 如果 ListBox 中的项数很大,则使用 PreferredHeight 属性的值调整控件大小可能会导致 ListBox 在窗体或 ListBox 容器的工作区之外大小。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.

适用于

另请参阅