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

屬性值

控制項中所有項目的結合高度 (單位為像素)。

屬性

範例

下列程式碼範例示範如何根據 屬性的值 PreferredHeight 來設定 SizeListBox 屬性,以便在 中顯示所有專案 ListBox 而不使用捲軸。 此範例要求 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 容器的工作區之外調整大小。

適用於

另請參閱