ListBox.HorizontalExtent 屬性

定義

取得或設定 ListBox 之水平捲軸可以捲動的寬度。

public:
 property int HorizontalExtent { int get(); void set(int value); };
public int HorizontalExtent { get; set; }
member this.HorizontalExtent : int with get, set
Public Property HorizontalExtent As Integer

屬性值

水平捲軸可以捲動控制項的寬度 (單位為像素)。 預設值是零。

範例

下列程式碼範例示範如何使用 HorizontalScrollbarHorizontalExtent 屬性來顯示顯示 控制項中 ListBox 所有專案文字的水準捲軸。 此範例也會使用 IntegralHeight 屬性來確保專案不會因為控制項的大小 ListBox 而部分顯示。 此範例要求 ListBox 已將名為 listBox1 的控制項新增至表單。

private:
   void DisplayHScroll()
   {
      // Make sure no items are displayed partially.
      listBox1->IntegralHeight = true;

      // Add items that are wide to the ListBox.
      for ( int x = 0; x < 10; x++ )
      {
         listBox1->Items->Add( String::Format( "Item {0} is a very large value that requires scroll bars", x ) );

      }

      // Display a horizontal scroll bar.
      listBox1->HorizontalScrollbar = true;

      // Create a Graphics object to use when determining the size of the largest item in the ListBox.
      Graphics^ g = listBox1->CreateGraphics();

      // Determine the size for HorizontalExtent using the MeasureString method using the last item in the list.
      int hzSize = (int)g->MeasureString( dynamic_cast<String^>(listBox1->Items[ listBox1->Items->Count - 1 ]), listBox1->Font ).Width;

      // Set the HorizontalExtent property.
      listBox1->HorizontalExtent = hzSize;
   }
private void DisplayHScroll()
{
   // Make sure no items are displayed partially.
   listBox1.IntegralHeight = true;

   // Add items that are wide to the ListBox.
   for (int x = 0; x < 10; x++)
   {
      listBox1.Items.Add("Item  " + x.ToString() + " is a very large value that requires scroll bars");
   }

   // Display a horizontal scroll bar.
   listBox1.HorizontalScrollbar = true;

   // Create a Graphics object to use when determining the size of the largest item in the ListBox.
   Graphics g = listBox1.CreateGraphics();

   // Determine the size for HorizontalExtent using the MeasureString method using the last item in the list.
   int hzSize = (int) g.MeasureString(listBox1.Items[listBox1.Items.Count -1].ToString(),listBox1.Font).Width;
   // Set the HorizontalExtent property.
   listBox1.HorizontalExtent = hzSize;
}
Private Sub DisplayHScroll()
     ' Make sure no items are displayed partially.
   listBox1.IntegralHeight = True
   Dim x As Integer

   ' Add items that are wide to the ListBox.
   For x = 0 To 10
      listBox1.Items.Add("Item  " + x.ToString() + " is a very large value that requires scroll bars")
   Next x

   ' Display a horizontal scroll bar.
   listBox1.HorizontalScrollbar = True

   ' Create a Graphics object to use when determining the size of the largest item in the ListBox.
   Dim g As System.Drawing.Graphics = listBox1.CreateGraphics()


   ' Determine the size for HorizontalExtent using the MeasureString method using the last item in the list.
   Dim hzSize As Integer = g.MeasureString(listBox1.Items(listBox1.Items.Count - 1).ToString(), listBox1.Font).Width
   ' Set the HorizontalExtent property.
   listBox1.HorizontalExtent = hzSize
End Sub

備註

如果 屬性設定 true 為 , HorizontalScrollbar 則此屬性只會報告有用的值。 如果 的 ListBox 寬度小於此屬性的值,則水準捲軸會水準捲動 中的 ListBox 專案。 如果 的 ListBox 寬度等於或大於此值,則會隱藏水準捲軸。 這個屬性的值不會由 ListBox 動態更新。 當 的專案是擁有者繪製時, ListBox 這個屬性會很有用。 例如,如果 的 ListBox 擁有者繪製專案寬度為 200 圖元,但 ListBox 寬度為 60 圖元,則 HorizontalExtent 屬性必須設定為 200,才能將專案的右邊緣捲動至控制項的可見區域。

適用於

另請參閱