通过检查是否在VB.Net的CheckListBox中切换来创建/删除对象

问题描述 投票:1回答:1

我在CheckBoxes中玩一些CheckedListItem,并且在确定何时通过其索引位置检查和/或取消选中特定选中的产品时遇到问题。我从MsgBox调试中知道,即使我尝试使用仅针对该特定产品执行的逻辑,如果我选择“产品通用清洁剂”或“清洁湿巾”,它将调用这两种方法的MsgBoxes。它以某种方式认为我同时选择了两个项目?

所以本质上我正在尝试这样做:

如果CheckedListBox中的项目为零调用合格菜单以获得所需的数量,然后返回到订购菜单。未选中CheckedListBox中的ElseIF项目零从当前订单中将其删除。

这实际上是冲洗后,对CheckedListBox中的所有项目重复进行。我怀疑VB.NET导致我的代码“掉线了”,尽管我试图防止这种情况,但它认为我选择的所有项目都是相同的。

我可以对此有什么想法吗?

谢谢。

Private Sub CleaningProductsList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CleaningProductsList.SelectedIndexChanged

    Dim allPurposeCleaner = New AllPurposeCleaner()
    Dim cleaningCloths = New cleaningCloths()

    ' If checked.
    If CleaningProductsList.GetItemChecked(0) = True Then
        isChecked = True
        Me.Hide()
        ' MsgBox("All Purpose Cleaner Selected")
        AmountMenue.setGivenProduct(allPurposeCleaner)
        AmountMenue.Show()
        ' If unchecked.
    ElseIf CleaningProductsList.GetItemChecked(0) = False Then
        isChecked = False
        ' MsgBox("All Purpose Cleaner  UnSelected ")
        MsgBox(CleaningProductsList.GetItemChecked(0).ToString + " ALLPURPOSE UNCHECKED")
        AmountMenue.removedGivenProduct(allPurposeCleaner)
    End If

    ' If checked.
    If CleaningProductsList.GetItemChecked(1) = True Then
        Me.Hide()
        AmountMenue.setGivenProduct(cleaningCloths)
        MsgBox("cleaning cloths Selected ")
        AmountMenue.Show()
        ' If unchecked.
    ElseIf CleaningProductsList.GetItemChecked(1) = False And CleaningProductsList.CheckOnClick = False Then
        MsgBox("cleaning cloths  UnSelected ")
        MsgBox(CleaningProductsList.GetItemChecked(2).ToString + " Cleaning Cloths UNCHECKED")
        isChecked = False
        AmountMenue.removedGivenProduct(cleaningCloths)
    End If 
End Sub
vb.net list listbox toggle checkedlistbox
1个回答
0
投票

这是您如何确定CheckedListBox中的更改的示例:

Private checkedIndexes As New List(Of Integer)
Private checkedItems As New List(Of String)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    CheckedListBox1.Items.AddRange({"First", "Second", "Third", "Fourth", "Fifth"})
End Sub

Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    If e.NewValue = CheckState.Checked Then
        'An item is being checked.
        checkedIndexes.Add(e.Index)
        checkedItems.Add(CStr(CheckedListBox1.Items(e.Index)))
    Else
        'An item is being unchecked.
        checkedIndexes.Remove(e.Index)
        checkedItems.Remove(CStr(CheckedListBox1.Items(e.Index)))
    End If

    Label1.Text = $"Checked indexes: {String.Join(", ", checkedIndexes)}"
    Label2.Text = $"Checked items: {String.Join(", ", checkedItems)}"
End Sub

e参数通过Index属性告诉您正在更改的项目,以及通过CurrentValueNewValue属性向您更改的项目。

如果要在该事件处理程序中获得检查项目的完整列表,因为在完成更改之前引发了该事件,您需要从控件提供的列表开始,然后添加或删除当前项目:] >

Dim checkedItems = CheckedListBox1.CheckedItems.Cast(Of String)().ToList()

If e.NewValue = CheckState.Checked Then
    checkedItems.Add(CStr(CheckedListBox1.Items(e.Index)))
Else
    checkedItems.Remove(CStr(CheckedListBox1.Items(e.Index)))
End If

'Use checkedItems here.
© www.soinside.com 2019 - 2024. All rights reserved.