ListBox.ColumnWidth 属性

获取或设置多列 ListBox 中列的宽度。

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

语法

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

value = instance.ColumnWidth

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

/** @property */
public void set_ColumnWidth (int value)
public function get ColumnWidth () : int

public function set ColumnWidth (value : int)

属性值

控件中每列的宽度(以像素为单位)。默认值为 0。

异常

异常类型 条件

ArgumentException

给此属性分配一个小于零的值。

备注

如果将该值设置为零 (0),则将默认宽度分配给每列。如果 ListBox 是多列 ListBox,则此属性返回列表中每列的当前宽度。可以使用此属性确保多列 ListBox 中的每列都可正确显示其项。

示例

下面的代码示例演示如何使用 ColumnWidthMultiColumn 属性来创建可在多列中显示项的 ListBox 控件。此示例要求已将名为 listBox1ListBox 控件添加到窗体中。

Private Sub SetupMyMultiColumn()
   Dim x As Integer
   ' Add items to the ListBox.
   For x = 0 To 50
      ListBox1.Items.Add("Items " + x.ToString())
   Next x
   ' Display items in columns.
   ListBox1.MultiColumn = True
   ' Determine the width of the items in the list to get the best column width setting.
   Dim width As Integer = CInt(ListBox1.CreateGraphics().MeasureString(ListBox1.Items(ListBox1.Items.Count - 1).ToString(), _
      ListBox1.Font).Width)
   ' Set the column width based on the width of each item in the list.
   ListBox1.ColumnWidth = width
End Sub
private void SetupMyMultiColumn()
{
   // Add items to the ListBox.
   for (int x = 0; x < 50; x++)
   {
      listBox1.Items.Add("Items " + x.ToString());
   }
   // Display items in columns.
   listBox1.MultiColumn = true;
   // Determine the width of the items in the list to get the best column width setting.
   int width = (int) listBox1.CreateGraphics().MeasureString(listBox1.Items[listBox1.Items.Count -1].ToString(),
      listBox1.Font).Width;
   // Set the column width based on the width of each item in the list.
   listBox1.ColumnWidth = width;
}
private:
   void SetupMyMultiColumn()
   {
      // Add items to the ListBox.
      for ( int x = 0; x < 50; x++ )
      {
         listBox1->Items->Add( String::Format( "Items {0}", x ) );

      }
      listBox1->MultiColumn = true;

      // Determine the width of the items in the list to get the best column width setting.
      int width = (int)listBox1->CreateGraphics()->MeasureString( listBox1->Items[ listBox1->Items->Count - 1 ]->ToString(), listBox1->Font ).Width;

      // Set the column width based on the width of each item in the list.
      listBox1->ColumnWidth = width;
   }
private void SetupMyMultiColumn()
{
    // Add items to the ListBox.
    for (int x = 0; x < 50; x++) {
        listBox1.get_Items().Add("Items " + System.Convert.ToString(x));
    }

    // Display items in columns.
    listBox1.set_MultiColumn(true);

    // Determine the width of the items in the list to get the best column
    // width setting.
    int width = (int)(listBox1.CreateGraphics().MeasureString(listBox1.
        get_Items().get_Item((listBox1.get_Items().get_Count() - 1)).
        ToString(), listBox1.get_Font()).get_Width());

    // Set the column width based on the width of each item in the list.
    listBox1.set_ColumnWidth(width);
} //SetupMyMultiColumn

平台

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 命名空间