Share via


ListBox.HorizontalExtent プロパティ

ListBox の水平スクロール バーでスクロールできる幅を取得または設定します。

Public Property HorizontalExtent As Integer
[C#]
public int HorizontalExtent {get; set;}
[C++]
public: __property int get_HorizontalExtent();public: __property void set_HorizontalExtent(int);
[JScript]
public function get HorizontalExtent() : int;public function set HorizontalExtent(int);

プロパティ値

水平スクロール バーでコントロールをスクロールできる幅 (ピクセル単位)。既定値は 0 です。

解説

このプロパティは、 HorizontalScrollbar プロパティが true に設定されている場合にだけ有効な値を報告します。 ListBox の幅がこのプロパティの値よりも小さい場合は、水平スクロール バーを使用して ListBox 内の項目を水平方向にスクロールできます。 ListBox の幅がこの値以上の場合は、水平スクロール バーは表示されません。このプロパティの値は、 ListBox によって動的には更新されません。このプロパティは、 ListBox の項目がオーナー描画である場合に役立ちます。たとえば、 ListBox のオーナー描画項目の幅が 200 ピクセルで、 ListBox の幅が 60 ピクセルの場合は、コントロールをスクロールして項目の右端を表示できるようにするためには、 HorizontalExtent プロパティを 200 に設定する必要があります。

使用例

[Visual Basic, C#, C++] HorizontalScrollbar プロパティと HorizontalExtent プロパティを使用して、 ListBox コントロールのすべての項目のテキストを表示するための水平スクロール バーを表示する方法を次の例に示します。また、この例では、 IntegralHeight プロパティを使用して、 ListBox コントロールのサイズにかかわらず、項目の全部が表示されることを保証します。この例は、 listBox1 という名前の ListBox コントロールがフォームに追加されていることを前提にしています。

 
Private Sub DisplayHScroll()
   ' Make no partial items are displayed vertically.
   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

[C#] 
private void DisplayHScroll()
{
   // Make no partial items are displayed vertically.
   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;
}

[C++] 
private:
   void DisplayHScroll() {
      // Make no partial items are displayed vertically.
      listBox1->IntegralHeight = true;

      // Add items that are wide to the ListBox.
      for (int x = 0; x < 10; x++) {
         listBox1->Items->Add( String::Format( S"Item {0} is a very large value that requires scroll bars", __box(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 __gc *>(listBox1->Items->Item[listBox1->Items->Count -1]), listBox1->Font).Width;
      // Set the HorizontalExtent property.
      listBox1->HorizontalExtent = hzSize;
   }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

ListBox クラス | ListBox メンバ | System.Windows.Forms 名前空間 | HorizontalScrollbar