ListBox.HorizontalExtent 属性

定义

获取或设置 ListBox 的水平滚动条可滚动的宽度。Gets or sets the width by which the horizontal scroll bar of a ListBox can scroll.

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

属性值

水平滚动条可滚动控件的宽度,以像素为单位。The width, in pixels, that the horizontal scroll bar can scroll the control. 默认值为零。The default is zero.

示例

下面的代码示例演示如何使用 HorizontalScrollbarHorizontalExtent 属性来显示水平滚动条,其中显示 ListBox 控件中的所有项文本。The following code example demonstrates how to use the HorizontalScrollbar and HorizontalExtent properties to display a horizontal scroll bar that shows all item text in the ListBox control. 该示例还使用 IntegralHeight 属性,以确保由于 ListBox 控件的大小而未部分显示项。The example also uses the IntegralHeight property to ensure that items are not partially displayed due to the size of the ListBox control. 此示例要求已将名为 listBox1ListBox 控件添加到窗体中。This example requires that a ListBox control, named listBox1, has been added to a form.

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

注解

如果 HorizontalScrollbar 属性设置为 true,则此属性仅报告有用值。This property only reports a useful value if the HorizontalScrollbar property is set to true. 如果 ListBox 的宽度小于此属性的值,水平滚动条将水平滚动 ListBox中的项。If the width of the ListBox is smaller than the value of this property, the horizontal scroll bar horizontally scrolls items in the ListBox. 如果 ListBox 的宽度等于或大于此值,则会隐藏水平滚动条。If the width of the ListBox is equal to or greater than this value, the horizontal scroll bar is hidden. ListBox不会动态更新此属性的值。The value of this property is not dynamically updated by the ListBox. ListBox 的项是所有者描述时,此属性很有用。This property is useful when the items of the ListBox are owner-drawn. 例如,如果 ListBox 的所有者绘制的项的宽度为200像素,但 ListBox 为60像素宽,则需要将 HorizontalExtent 属性设置为200,以便将项的右边缘滚动到控件的可见区域。For example, if the owner drawn items of the ListBox are 200 pixels wide, but the ListBox is 60 pixels wide, the HorizontalExtent property would need to be set to 200 in order to scroll the right edge of the items into the visible region of the control.

适用于

另请参阅