This documentation is archived and is not being maintained.

ListBox.HorizontalExtent Property

Gets or sets the width by which the horizontal scroll bar of a ListBox can scroll.

[Visual Basic]
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);

Property Value

The width, in pixels, that the horizontal scroll bar can scroll the control. The default is zero.

Remarks

This property only reports a useful value if the HorizontalScrollbar property is set to true. If the width of the ListBox is smaller than the value of this property, the horizontal scroll bar horizontally scrolls items in the ListBox. If the width of the ListBox is equal to or greater than this value, the horizontal scroll bar is hidden. The value of this property is not dynamically updated by the ListBox. This property is useful when the items of the ListBox are owner-drawn. 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.

Example

[Visual Basic, C#, C++] The following 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 assumes that a ListBox control, named listBox1, has been added to a form.

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ListBox Class | ListBox Members | System.Windows.Forms Namespace | HorizontalScrollbar

Show: