我有一个选中的列表框,如果我检查一个项目,它将在列表框中显示已检查的项目,但仅当该项目存在于数据库表中时。一切正常,除非数据不在表格上,复选框仍保持选中状态。这是我的代码
Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
If e.NewValue = CheckState.Checked Then
'Database Query here
If data is on table then
Listbox1.items.add(CheckedListBox1.SelectedItem.ToString)
Else
CheckedListBox1.SetItemCheckState(CheckedListBox1.SelectedIndex, CheckState.Unchecked)
'Unchecking the checked checkbox because data isn't on the table, i'm having problems here, the checkbox remains checked.
end if
Else
Listbox1.items.remove(CheckedListBox1.SelectedItem.ToString)
End if
end sub
答案 0 :(得分:1)
处理CheckedListBox1.ItemCheck事件的CheckedListBox1_ItemCheck方法。
尝试使用传入方法的引用,而不是尝试单击项目的SetItemCheckState:
e.NewValue = CheckState.Unchecked
此外,您可能最好确保从已检查的项目中获取值,而不是引用SelectedItem:
CheckedListBox1.Items(e.Index).ToString()