ListBox.HorizontalScrollbar 属性

定义

获取或设置一个值,该值指示是否在控件中显示水平滚动条。Gets or sets a value indicating whether a horizontal scroll bar is displayed in the control.

public:
 property bool HorizontalScrollbar { bool get(); void set(bool value); };
public bool HorizontalScrollbar { get; set; }
member this.HorizontalScrollbar : bool with get, set
Public Property HorizontalScrollbar As Boolean

属性值

如果为 true,则在控件中显示水平滚动条;否则为 falsetrue to display a horizontal scroll bar in the control; otherwise, false. 默认值为 falseThe default is false.

示例

下面的代码示例演示如何使用 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 属性决定当 ListBox 内项的宽度超出控件的右边缘时,ListBox 是否应显示水平滚动条。The HorizontalScrollbar property determines whether the ListBox should display a horizontal scroll bar when the width of items within the ListBox extend beyond the right edge of the control. 如果将此属性设置为 "true",则将根据 ListBox中项的宽度自动显示滚动条。When this property is set to true, the scroll bar is automatically displayed based on the width of items in the ListBox. 如果 ListBox 是所有者描述的 ListBox,为了正确地显示水平滚动条,必须设置 HorizontalExtent 属性。If the ListBox is an owner-drawn ListBox, in order to properly display a horizontal scroll bar, you must set the HorizontalExtent property.

适用于

另请参阅