visual basic checkedlistbox到列表框

时间:2017-03-05 16:35:50

标签: vb.net

我是visual basic的新手,我正在尝试创建一个程序,其中checkedlistbox中的选定任务列出了列表框中的耗材列表,但我没有运气。 我的问题是:您是否可以通过检查checkedlistbox中的项目来调用存储在数组中的字符串以显示在列表框中? 我设置好了,所以当用户点击按钮时,任务列表会显示在checkedlistbox中(任务根据他们推送的按钮而变化),目标是让每个任务的供应都出现在列表框中。 我们最终将使用数据库,但是现在我试图让它与数组一起运行。 以下是用户按下客厅按钮时的当前代码:

Dim livRoomItems As String() = {"sofa", "TV", "entertainment center", "coffee table", "end table", "book shelf", "living room floor"}
'declare an array of strings for tasks
Dim livTasks As String() = {"remove cushions and vacuum couch", "dust front and back of the TV", "wipe down entertainment center with furniture polish",
                            "wipe down coffee table with furniture polish", "wipe down end table with furniture polish", "wipe down book shelf with furniture polish",
                            "vacuum living room floor"}
'declare an array of strings for all supplies for any room
Dim livSupplies As String() = {"all-in-one cleaning spray", "furniture polish", "broom/dustpan and mop", "vacuum", "glass cleaner", "duster", "toilet bowl cleaner", "shower cleaner", "canned air"}

当用户单击按钮时,livRoomItems中的所有项都出现在checkedlistbox cbxRoomItems中。

Private Sub btnLroom_Click(sender As Object, e As EventArgs) Handles btnLroom.Click

    'add array of items to the checklistbox for livingroom
    cbxRoomItems.Items.AddRange(livRoomItems)

End Sub

我正在尝试通过在所选项目的数组中调用正确的索引来获取与用户在cbxRoomItems中检查以显示在listbox lbxSupplies中的内容相关的耗材,然后在列表框lbxTasks中显示该项目的相关任务。< / p>

我能够在列表框中显示耗材数组,但不能显示已经检查过的内容。 这是我到目前为止所尝试的:

'这会将客厅供应阵列添加到列表框中 - 它可以工作,但有重复         “lbxSupplies.Items.AddRange(livSupplies)

    'This doesn't work 
    'For Each livRoomItem() As Array In cbxRoomItems.CheckedItems
    'lbxSupplies.Items.Add(supplies(0))
    'lbxSupplies.SelectedIndex = 0
    'Next

    'I tried this just for the first checkbox for living room, it does not work
    'If (cbxRoomItems.CheckOnClick = True) Then
    'livRoomItems(0) = lbxSupplies.Items.Add(supplies(3))
    'End If

    'determine the type of supplies needed for the checked living room items
    If (livRoomItems(0) = (cbxRoomItems.CheckOnClick = True)) Then
    lbxSupplies.Items.Add(supplies(3))
    ElseIf (livRoomItems(1) = (cbxRoomItems.CheckOnClick = True)) Then
    lbxSupplies.Items.Add(supplies(5))
    ElseIf (livRoomItems(2) = (cbxRoomItems.CheckOnClick = True)) Then
    lbxSupplies.Items.Add(supplies(1))
    ElseIf (livRoomItems(3) = (cbxRoomItems.CheckOnClick = True)) Then
    lbxSupplies.Items.Add(supplies(1))
    ElseIf (livRoomItems(4) = (cbxRoomItems.CheckOnClick = True)) Then
    lbxSupplies.Items.Add(supplies(1))
    ElseIf (livRoomItems(5) = (cbxRoomItems.CheckOnClick = True)) Then
    lbxSupplies.Items.Add(supplies(1))
    ElseIf (livRoomItems(6) = (cbxRoomItems.CheckOnClick = True)) Then
    lbxSupplies.Items.Add(supplies(3))
    End If
End Sub

再次 - 我对视觉基础很新,并且很难找到我想要做的参考。

我很欣赏这些反馈!!

更新

我很幸运: Dim indexChecked As Integer

    For Each indexChecked In cbxRoomItems.CheckedIndices
        lbxTasks.Items.Add(livTasks(indexChecked).ToString)
    Next

但是因为我不知道如何解决如何在复选框中添加另一个房间的数组。我可以把它们变成一个巨大的阵列,但是用户选择选择房间的按钮将毫无用处。

0 个答案:

没有答案