Domanda

I am working on a simple To Do List program, the user can type anything in a text box, press the button, and it adds the text as an item to a CheckedListBox. Now I want to add the text "Done" before each item if it is checked, and then remove the text if the user unchecks it.

Code:

 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

I can't seem to get it working. I've never dealt with CheckedListBoxes before.

È stato utile?

Soluzione

Very close! At the moment, your code is looking to see if the currently highlighted (not checked) item's text = "True".

Instead, we need to examine the ItemCheckedEventArgs parameter that's passed into the method:

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top