次の例では、 ColumnWidths プロパティを使用して、複数列の ListBox の列幅を変更します。 この例では、3 つの TextBox コントロールを使用して個々の列の幅を指定し、 Click イベントを使用して各 TextBox の測定単位を指定します。
この例を利用するには、次のコード例をフォームのスクリプト エディターにコピーします。 コードを実行するには、 Open イベントが生じるようにフォームを開く必要があります。 フォームには次のコントロールが含まれている必要があります。
リスト ボックス ( ListBox ) コントロール (ListBox1)
3 つのカスタム テキスト フィールド (Text1、Text2、および Text3)
カスタム テキスト フィールド (Text1、Text2、および Text3) とバインドした 3 つのテキスト ボックス ( TextBox ) コントロール (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 のサポートおよびフィードバックを参照してください。