ListBox.HorizontalExtent 属性

获取或设置 ListBox 的水平滚动条可滚动的宽度。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<LocalizableAttribute(True)> _
Public Property HorizontalExtent As Integer
用法
Dim instance As ListBox
Dim value As Integer

value = instance.HorizontalExtent

instance.HorizontalExtent = value
[LocalizableAttribute(true)] 
public int HorizontalExtent { get; set; }
[LocalizableAttribute(true)] 
public:
property int HorizontalExtent {
    int get ();
    void set (int value);
}
/** @property */
public int get_HorizontalExtent ()

/** @property */
public void set_HorizontalExtent (int value)
public function get HorizontalExtent () : int

public function set HorizontalExtent (value : int)

属性值

水平滚动条可滚动控件的宽度,以像素为单位。默认值为零。

备注

仅当 HorizontalScrollbar 属性设置为 true 时,此属性才报告有用的值。如果 ListBox 的宽度小于此属性的值,则水平滚动条水平滚动 ListBox 中的项。如果 ListBox 的宽度等于或大于此值,则隐藏水平滚动条。此属性的值不由 ListBox 动态更新。当 ListBox 的项是所有者描述的项时,此属性很有用。例如,如果 ListBox 的所有者描述的项为 200 个像素宽,但 ListBox 为 60 个像素宽,则 HorizontalExtent 属性将需要设置为 200,以便将这些项的右边缘滚动到控件的可见区域内。

示例

下面的代码示例演示如何使用 HorizontalScrollbarHorizontalExtent 属性来显示用于在 ListBox 控件中显示所有项文本的水平滚动条。该示例还使用 IntegralHeight 属性以确保不会因为 ListBox 控件的大小而不能完整地显示项。此示例要求已将名为 listBox1ListBox 控件添加到窗体中。

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
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;
}
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( "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 no partial items are displayed vertically.
    listBox1.set_IntegralHeight(true);

    // Add items that are wide to the ListBox.
    for (int x = 0; x < 10; x++) {
        listBox1.get_Items().Add("Item  " + Convert.ToString(x) 
            + " is a very large value that requires scroll bars");
    }

    // Display a horizontal scroll bar.
    listBox1.set_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.get_Items().
        get_Item(listBox1.get_Items().get_Count() - 1).ToString(), 
        listBox1.get_Font()).get_Width());

    // Set the HorizontalExtent property.
    listBox1.set_HorizontalExtent(hzSize);
} //DisplayHScroll

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ListBox 类
ListBox 成员
System.Windows.Forms 命名空间
HorizontalScrollbar