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)

'Declaration
Public Property HorizontalExtent As Integer

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft