通过


ListBox.PreferredHeight 属性

定义

获取 .. 中 ListBox所有项的组合高度。

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

属性值

控件中所有项的组合高度(以像素为单位)。

属性

示例

下面的代码示例演示如何根据属性的值设置Size属性ListBox,以便在不使用滚动条的情况下显示所有项ListBoxPreferredHeight 此示例要求 ListBox 已将名为“的 listBox1控件”添加到窗体中。

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

适用于

另请参阅