0

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much for your tidying up of my code. Still learning new things with vb.net. What exactly is the variable "e"?
Every Form Control Event method in .Net passes in two parameters: "Sender" is the object that is raising the event. e.g. if you have 1000 buttons on your form, "Sender" is the actual button that was clicked, so you don't have to try and guess which button was clicked. "e" is information about the event. So for example, Button.MouseMove would have an "e" parameter containing the mouse's X and Y position. Different events have different "e" parameters, containing interesting information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.