選択されている項目を取得する

スポンサーリンク

選択されている項目を取得するには SelectedItems プロパティを参照します。SelectedItems プロパティには、選択された順番に項目が格納されています。

下記は[選択項目を取得する]ボタンがクリックされたときに、選択状態になっている項目をメッセージボックスに表示する例です。

選択されている項目を取得する例

 

VBの例

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Dim selectItem As String = String.Empty

    '選択項目を取得し1つの文字列としてまとめる
    For Each Item As ListBoxItem In ListBox1.SelectedItems
        selectItem &= Item.Content & vbCrLf
    Next

    MessageBox.Show(selectItem)
End Sub

C#の例

private void button1_Click(object sender, RoutedEventArgs e)
{
    string selectItem = string.Empty;

    // 選択項目を取得し1つの文字列としてまとめる
    foreach ( ListBoxItem item in listBox1.SelectedItems )
    {
        selectItem += item.Content + "\n";
    }

    MessageBox.Show(selectItem);
}
タイトルとURLをコピーしました