Share via


MultiSelect プロパティ、Selected プロパティの例

次の例では、 MultiSelect プロパティと Selected プロパティを使用して、ユーザーが ListBox 内の 1 つ以上の項目を選択する方法を示します。

ユーザーは、オプション ボタンを選択して選択方法を指定し、 ListBox から項目を選択します。 ユーザーは、CommandButton をクリックして、2 つ目の ListBox で選択した項目を表示できます。

この例を使用するには、以下のサンプル コードをフォームの宣言部分にコピーします。 フォームに次が含まれていることを確認してください。

  • 2 つのリスト ボックス ( ListBox ) コントロール (ListBox1 と ListBox2)
  • CommandButton1 と命名された CommandButton
  • 3 つのオプション ボタン ( OptionButton ) コントロール (OptionButton1、OptionButton2、OptionButton3)
Dim i As Integer 
 
Private Sub CommandButton1_Click() 
 ListBox2.Clear 
 
 For i = 0 To 9 
 If ListBox1.Selected(i) = True Then 
 ListBox2.AddItem ListBox1.List(i) 
 End If 
 Next i 
 
End Sub 
 
Private Sub OptionButton1_Click() 
 ListBox1.MultiSelect = fmMultiSelectSingle 
End Sub 
 
Private Sub OptionButton2_Click() 
 ListBox1.MultiSelect = fmMultiSelectMulti 
End Sub 
 
Private Sub OptionButton3_Click() 
 ListBox1.MultiSelect = fmMultiSelectExtended 
End Sub 
 
Private Sub UserForm_Initialize() 
 For i = 0 To 9 
 ListBox1.AddItem "Choice " & (ListBox1.ListCount + 1) 
 Next i 
 
 OptionButton1.Caption = "Single Selection" 
 ListBox1.MultiSelect = fmMultiSelectSingle 
 OptionButton1.Value = True 
 
 OptionButton2.Caption = "Multiple Selection" 
 OptionButton3.Caption = "Extended Selection" 
 
 CommandButton1.Caption = "Show selections" 
 CommandButton1.AutoSize = True 
End Sub

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。