ColumnWidths Property

Applies To

ComboBox control, ListBox control.

Description

Specifies the width of each column in a multicolumn combo box or list box.

Syntax

object.ColumnWidths [= String]

The ColumnWidths property syntax has these parts:

Part

Description

object

Required. A valid object.

String

Optional. Sets the column width in points. A setting of –1 or blank results in a calculated width. A width of 0 hides a column. To specify a different unit of measurement, include the unit of measure. A value greater than 0 explicitly specifies the width of the column.


Settings

To separate column entries, use semicolons (;) as list separators. Or use the list separator specified in the Regional Settings section of the Windows Control Panel.

Any or all of the ColumnWidths property settings can be blank. You create a blank setting by typing a list separator without a preceding value.

If you specify a –1 in the property page, the displayed value in the property page is a blank.

To calculate column widths when ColumnWidths is blank or –1, the width of the control is divided equally among all columns of the list. If the sum of the specified column widths exceeds the width of the control, the list is left-aligned within the control and one or more of the rightmost columns are not displayed. Users can scroll the list using the horizontal scroll bar to display the rightmost columns.

The minimum calculated column width is 72 points (1 inch). To produce columns narrower than this, you must specify the width explicitly.

Unless specified otherwise, column widths are measured in points. To specify another unit of measure, include the units as part of the values. The following examples specify column widths in several units of measure and describe how the various settings would fit in a three-column list box that is 4 inches wide.

Setting

Effect

90;72;90

The first column is 90 points (1.25 inch); the second column is 72 points (1 inch); the third column is 90 points.

6 cm;0;6 cm

The first column is 6 centimeters; the second column is hidden; the third column is 6 centimeters. Because only part of the third column is visible, a horizontal scroll bar appears.

1.5 in;0;2.5 in

The first column is 1.5 inches, the second column is hidden, and the third column is 2.5 inches.

2 in;;2 in

The first column is 2 inches, the second column is 1 inch (default), and the third column is 2 inches. Because only half of the third column is visible, a horizontal scroll bar appears.

(Blank)

All three columns are the same width (1.33 inches).


Remarks

In a combo box, the system displays the column designated by the TextColumn property in the text box portion of the control.

See Also

ColumnCount property, ColumnHeads property, TextColumn property.

Example

The following example uses the ColumnWidths property to change the column widths of a multicolumn ListBox. The example uses three TextBox controls to specify the individual column widths and uses the Exit event to specify the units of measure of each TextBox.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

  • A ListBox named ListBox1.
  • Three TextBox controls named TextBox1 through TextBox3.
  • A CommandButton named CommandButton1.
Try entering the value 0 to hide a column.

Dim MyArray(2, 3) As String

Private Sub CommandButton1_Click()
    'ColumnWidths requires a value for each column separated by semicolons
    ListBox1.ColumnWidths = TextBox1.Text & ";" & TextBox2.Text & ";" _
& TextBox3.Text End Sub Private Sub TextBox1_Exit(ByVal Cancel as MSForms.ReturnBoolean) 'ColumnWidths accepts points (no units), inches or centimeters;
'make inches the default If Not (InStr(TextBox1.Text, "in") > 0 Or InStr(TextBox1.Text, "cm") _
> 0) Then TextBox1.Text = TextBox1.Text & " in" End If End Sub Private Sub TextBox2_Exit(ByVal Cancel as MSForms.ReturnBoolean) 'ColumnWidths accepts points (no units), inches or centimeters; make
'inches the default If Not (InStr(TextBox2.Text, "in") > 0 Or InStr(TextBox2.Text, "cm") _
> 0) Then TextBox2.Text = TextBox2.Text & " in" End If End Sub Private Sub TextBox3_Exit(ByVal Cancel as MSForms.ReturnBoolean) 'ColumnWidths accepts points (no units), inches or centimeters; make
'inches the default If Not (InStr(TextBox3.Text, "in") > 0 Or InStr(TextBox3.Text, "cm") _
> 0) Then TextBox3.Text = TextBox3.Text & " in" End If End Sub Private Sub UserForm_Initialize() Dim i, j, Rows As Single ListBox1.ColumnCount = 3 Rows = 2 For j = 0 To ListBox1.ColumnCount - 1 For i = 0 To Rows - 1 MyArray(i, j) = "Row " & i & ", Column " & j Next i Next j ListBox1.List() = MyArray 'Load MyArray into ListBox1 TextBox1.Text = "1 in" '1-inch columns initially TextBox2.Text = "1 in" TextBox3.Text = "1 in" End Sub