无法从vb.net中的checkedlistbox输出

时间:2013-08-23 13:18:44

标签: vb.net checkbox tchecklistbox

我只是在学习vb,在输出文本框中的特定选中项时遇到一些问题。我尝试了很多方法,但仍然无法找到正确的方法,也许我忘记了什么?

    Private Sub submitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submitBtn.Click
    Dim output As String
    Dim listOfProd As String
    listOfProd = ""


    output = "Selected Region: " + destination.Text + Environment.NewLine + "Seleted Place: " +
        places.SelectedItem + Environment.NewLine + "Accomodation: " +
        accomodation.Text + Environment.NewLine + "Products Selected: " + Environment.NewLine

    For i As Integer = 0 To products.Items.Count
        If products.Items(i) Is products.CheckedItems Then
            listOfProd = listOfProd + products.Items(i)

        End If
    Next

    output = output + listOfProd

    outputHere.Text = output

1 个答案:

答案 0 :(得分:1)

不要循环并确定选中哪个复选框列表项,只需使用CheckedItems集合,如下所示:

' Loop through only the items that are checked 
For Each itemChecked In products.CheckedItems
    ' Get the text of the selected item and append it to the output string
    listOfProd = listOfProd + itemChecked.ToString()
Next