我正在处理一个简单的待办事项列表程序,用户可以在文本框中键入任何内容,按下按钮,然后将文本作为项添加到CheckedListBox。现在我想在每个项目之前添加文本“Done”(如果已选中),然后在用户取消选中时删除文本。
代码:
Private Sub MyCbList_ItemCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles MyCbList.ItemCheck
If MyCbList.Items.Item(MyCbList.SelectedIndex) = True Then
MyCbList.Items.Item(MyCbList.SelectedIndex) = "Done: " + MyCbList.Items.Item(MyCbList.SelectedIndex)
Else
MyCbList.Items.Item(MyCbList.SelectedIndex) = MyCbList.Items.Item(MyCbList.SelectedIndex).Replace("Done: ", "")
End If
End Sub
我似乎无法让它发挥作用。我以前从未处理过CheckedListBoxes。
答案 0 :(得分:1)
非常接近!目前,您的代码正在查看当前突出显示(未选中)项目的text =“True”。
相反,我们需要检查传递给方法的ItemCheckedEventArgs参数:
Private Sub MyCbList_ItemCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles MyCbList.ItemCheck
If e.NewValue = CheckState.Checked Then
MyCbList.Items.Item(e.Index) = "Done: " + MyCbList.Items.Item(MyCbList.SelectedIndex)
Else
MyCbList.Items.Item(e.Index) = MyCbList.Items.Item(MyCbList.SelectedIndex).Replace("Done: ", "")
End If
End Sub