共用方式為


變更多欄清單方塊的資料行寬度

下列範例會使用 ColumnWidths 屬性來變更多欄 ListBox的資料行寬度。 此範例使用三個 TextBox 控制項來指定個別的資料行寬度,並使用 Click 事件來指定每個 TextBox的量值單位。

若要使用本範例,請將此範例程式碼複製到表單的指令碼編輯器中。 若要執行該程式碼,您需要開啟該表單以啟動 Open 事件。 請確定表單包含:

  • 一個名為 ListBox1 的 ListBox

  • 三個名稱分別為 Text1、Text2 和 Text3 的自訂文字欄位。

  • 三個名稱分別為 TextBox1、TextBox2 和 TextBox3 的 文字方塊 控制項,這些控制項結合到上述自訂文字欄位上。

  • 一個名為 CommandButton1 的 CommandButton

嘗試輸入 0 值來隱藏欄。

Dim MyArray(2, 3) 
Dim ListBox1 
Dim TextBox1 
Dim TextBox2 
Dim TextBox3 
Dim CommandButton1 
 
Sub Item_Open() 
Dim i, j, Rows 
 
Set ListBox1 = Item.GetInspector.ModifiedFormPages("P.2").ListBox1 
Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").TextBox1 
Set TextBox2 = Item.GetInspector.ModifiedFormPages("P.2").TextBox2 
Set TextBox3 = Item.GetInspector.ModifiedFormPages("P.2").TextBox3 
Set CommandButton1 = Item.GetInspector.ModifiedFormPages("P.2").CommandButton1 
 
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 
Next 
 
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 
 
Sub CommandButton1_Click() 
 'ColumnWidths requires a value for each column separated by semicolons 
 ListBox1.ColumnWidths = TextBox1.Text & ";" & TextBox2.Text & ";" & TextBox3.Text 
End Sub 
 
Sub Item_CustomPropertyChange(ByVal Name) 
msgbox Name 
Select Case Name 
Case "Text1" 
 '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 
Case "Text2" 
 '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 
Case "Text3" 
 '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 Select 
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應