2012-11-15 2 views
1

에 저장하는 방법 모든 체크 된 항목을 문자열로 저장합니다.이 방법은 완벽하게 작동하지만 체크 된 모든 항목을 이름이있는 배열에 저장하려고합니다.CheckedListBox의 배열에 체크 된 항목을 vb.net의

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 

도와주세요!

답변

0

이렇게해야합니다.

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 
+0

감사 (2, 3 항목이 선택되어있는 목록에서 4 개 항목을 가정) .. :) –

1

당신은 왜 당신이 대신 Items을 사용하는 CheckedItems이 필요하면? 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)) 항목의 텍스트를 표시합니다.

같은

그래서 생성 할 출력 : 나를 위해 작동 ... 답장을

Checked Items: Item 2 = Apple 
Checked Items: Item 3 = Banana 
관련 문제