ListBox.IntegralHeight 属性

定义

获取或设置一个值,该值指示控件是否应调整大小以避免只显示项的局部。Gets or sets a value indicating whether the control should resize to avoid showing partial items.

public:
 property bool IntegralHeight { bool get(); void set(bool value); };
public bool IntegralHeight { get; set; }
member this.IntegralHeight : bool with get, set
Public Property IntegralHeight As Boolean

属性值

如果该控件调整大小以便不显示不完整的项,则为 true;否则为 falsetrue if the control resizes so that it does not display partial items; otherwise, false. 默认为 trueThe default is true.

示例

下面的代码示例演示如何使用 HorizontalScrollbarHorizontalExtent 属性来显示水平滚动条,其中显示 ListBox 控件中的所有项文本。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. 该示例还使用 IntegralHeight 属性,以确保由于 ListBox 控件的大小而未部分显示项。The example also uses the IntegralHeight property to ensure that items are not partially displayed due to the size of the ListBox control. 此示例要求已将名为 listBox1ListBox 控件添加到窗体中。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;
   }
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("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;
}
Private Sub DisplayHScroll()
     ' Make sure no items are displayed partially.
   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

注解

如果将此属性设置为 true,则控件会自动调整大小,以确保不会部分显示某个项。When this property is set to true, the control automatically resizes to ensure that an item is not partially displayed. 如果要根据窗体的空间要求来维护 ListBox 的原始大小,请将此属性设置为 "false"。If you want to maintain the original size of the ListBox based on the space requirements of your form, set this property to false.

默认情况下,ListBoxCheckedListBox 大小均为仅显示整个项。By default, the ListBox and the CheckedListBox sizes are such that they show only whole items. 如果希望 ListBoxCheckedListBox 完全填充停靠区域,请将 "IntegralHeight 设置为" false"。If you want the ListBox or CheckedListBox to completely fill a docked area, set IntegralHeight to false. 这将导致控件完全填充区域,但不会完全显示最后一项。This causes the control to completely fill the area, but the last item is not fully displayed.

如果 ListBox 不包含任何项,则此属性不起作用。If the ListBox does not contain any items, this property has no effect.

备注

整数高度取决于 ListBox的高度,而不是客户端区域的高度。The integral height is based on the height of the ListBox, rather than the client area height. 因此,当 IntegralHeight 属性设置 true时,如果显示滚动条,则仍可以部分显示项。As a result, when the IntegralHeight property is set true, items can still be partially shown if scroll bars are displayed.

备注

如果 DrawMode 属性设置为 DrawMode.OwnerDrawVariable,则此属性不起作用。If the DrawMode property is set to DrawMode.OwnerDrawVariable, this property has no effect.

适用于

另请参阅