Export (0) Print
Expand All

ListBox.ColumnWidth Property

Gets or sets the width of columns in a multicolumn ListBox.

[Visual Basic]
Public Property ColumnWidth As Integer
[C#]
public int ColumnWidth {get; set;}
[C++]
public: __property int get_ColumnWidth();
public: __property void set_ColumnWidth(int);
[JScript]
public function get ColumnWidth() : int;
public function set ColumnWidth(int);

Property Value

The width, in pixels, of each column in the control. The default is 0.

Exceptions

Exception Type Condition
ArgumentException A value less than zero is assigned to the property.

Remarks

If you set the value to zero (0), a default width is assigned to each column. If the ListBox is a multicolumn list box, this property returns the current width of each column in the list. You can use this property to ensure that each column in a multicolumn ListBox can properly display its items.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the ColumnWidth and MultiColumn properties to create a ListBox control that displays items in multiple columns. This example assumes that a ListBox control, named listBox1, has been added to a form.

[Visual Basic] 
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

[C#] 
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;
}

[C++] 
private:
   void SetupMyMultiColumn()
   {
      // Add items to the ListBox.
      for (int x = 0; x < 50; x++)
      {
         listBox1->Items->Add(String::Format( S"Items {0}", __box(x)));
      }
      // 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->Item[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;
   }

[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

Show:
© 2015 Microsoft