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

属性值

Int32

水平滚动条可滚动控件的宽度,以像素为单位。 默认值为零。

示例

下面的代码示例演示如何使用 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

注解

如果属性设置为 trueHorizontalScrollbar则此属性仅报告有用的值。 如果宽度 ListBox 小于此属性的值,水平滚动条将水平滚动条中的 ListBox项。 如果宽度 ListBox 等于或大于此值,则隐藏水平滚动条。 此属性的值不会由 ListBox. 当所有者绘制的 ListBox 项时,此属性非常有用。 例如,如果所有者绘制的 ListBox 项宽为 200 像素,但 ListBox 宽度为 60 像素,则需要将 HorizontalExtent 属性设置为 200,以便将项的右边缘滚动到控件的可见区域。

适用于

另请参阅