Export (0) Print
Expand All

ListBox.IntegralHeight Property

Gets or sets a value indicating whether the control should resize to avoid showing partial items.

[Visual Basic]
Public Property IntegralHeight As Boolean
[C#]
public bool IntegralHeight {get; set;}
[C++]
public: __property bool get_IntegralHeight();
public: __property void set_IntegralHeight(bool);
[JScript]
public function get IntegralHeight() : Boolean;
public function set IntegralHeight(Boolean);

Property Value

true if the control resizes so that it does not display partial items; otherwise, false. The default is true.

Remarks

When this property is set to true, the control automatically resizes to ensure that an item is not partially displayed. If you want to maintain the original size of the ListBox based on the space requirements of your form, set this property to false. If the ListBox does not contain any items, this property has no effect.

Note   The integral height is based on the height of the ListBox, rather than the client area height. As a result, when the IntegralHeight property is set true, items can still be partially shown if scroll bars are displayed.
Note   If the DrawMode property is set to DrawMode.OwnerDrawVariable, this property has no effect.

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

Show:
© 2014 Microsoft