Question Editing a text file problem

Nucleus

Active member
Joined
May 24, 2005
Messages
30
Programming Experience
Beginner
I have a CheckedListBox and I want to use it for editing a text file. This is what I have so far.

VB.NET:
    Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged

        If CheckedListBox1.CheckedItems(0) = "test1" Then
            IO.File.WriteAllText("C:\test.txt", IO.File.ReadAllText("C:\test.txt").Replace("test1", "yeah"))
        Else
            IO.File.WriteAllText("C:\test.txt", IO.File.ReadAllText("C:\test.txt").Replace("yeah", "test1"))
        End If

    End Sub

When I click on the first checkbox it generates the below error though.

Help?
 

Attachments

  • Untitled.png
    Untitled.png
    9.7 KB · Views: 26
You do know that there's a difference between an item being selected and an item being checked, right? Selection in a CheckedListBox is exactly the same as selection in a regular ListBox. Checking is obviously something different. If you're using a CheckedListBox then most likely you are interested in checking, not selecting, so you need to make sure that you are using the appropriate members. From the name, does SelectedIndexChanged sound appropriate?
 
The exception is most likely a result of there being no items checked when you use CheckedListBox1.CheckedItems(0). As jmcihinney mentioned, there is a difference between selected and checked when using this control. In your example, you are handling the selected event, but then performing logic based on the checked items. Review what you are ultimately trying to do and make sure you're working with the correct event.
 
Back
Top