Export (0) Print
Expand All

ListBox.HorizontalExtent Property

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

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

public int HorizontalExtent { get; set; }

Property Value

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

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.

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 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;
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft