ListBox::HorizontalScrollbar Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets a value indicating whether a horizontal scroll bar is displayed in the control.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public:
property bool HorizontalScrollbar {
	bool get();
	void set(bool value);
}

Property Value

Type: System::Boolean

true to display a horizontal scroll bar in the control; otherwise, false. The default is false.

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. When this property is set to true, the scroll bar is automatically displayed based on the width of items in the ListBox. If the ListBox is an owner-drawn ListBox, in order to properly display a horizontal scroll bar, you must set the HorizontalExtent property.

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. The example also uses the IntegralHeight property to ensure that items are not partially displayed due to the size of the ListBox control. 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;
   }

.NET Framework
Available since 1.1
Return to top
Show: