1

すべてのチェック済みアイテムを文字列に保存しています。これは私にとっては完璧に機能しますが、すべてのチェック済みアイテムを名前とともに配列に保存したいと思います。

 Dim i As Integer

 Dim ListItems As String

        ListItems = "Checked Items:" & ControlChars.CrLf

        For i = 0 To (ChkListForPrint.Items.Count - 1)
            If ChkListForPrint.GetItemChecked(i) = True Then
                ListItems = ListItems & "Item " & (i + 1).ToString & " = " & ChkListForPrint.Items(i)("Name").ToString & ControlChars.CrLf
            End If
        Next

助けてください!

4

2 に答える 2

1

これでうまくいくはずです。

Dim ListItems as New List(Of String)
For i = 0 To (ChkListForPrint.Items.Count - 1)
    If ChkListForPrint.GetItemChecked(i) = True Then
       ListItems.Add(ChkListForPrint.Items(i)("Name").ToString)
    End If
Next
于 2012-11-15T08:19:22.293 に答える
1

必要な場合はCheckedItems、なぜItems代わりに使用しているのですか?を使用することをお勧めしますCheckedItems

私はあなたのコードを少し変更しました、そしてこのような何かがあなたを助けるでしょう:

Dim collection As New List(Of String)()        ' collection to store check items
Dim ListItems As String = "Checked Items: "    ' A prefix for any item

For i As Integer = 0 To (ChkListForPrint.CheckedItems.Count - 1)  ' iterate on checked items
    collection.Add(ListItems & "Item " & (ChkListForPrint.Items.IndexOf(ChkListForPrint.CheckedItems(i)) + 1).ToString & " = " & ChkListForPrint.GetItemText(ChkListForPrint.CheckedItems(i)).ToString)  ' Add to collection
Next

ここ:

  1. ChkListForPrint.Items.IndexOf(ChkListForPrint.CheckedItems(i)) チェックされたアイテムのインデックスを取得します。

  2. ChkListForPrint.GetItemText(ChkListForPrint.CheckedItems(i))アイテムのテキストが表示されます。

したがって、次のような出力が生成されます。(2つと3つの項目がチェックされているリスト内の4つの項目を想定)

Checked Items: Item 2 = Apple
Checked Items: Item 3 = Banana
于 2012-11-15T10:53:28.370 に答える