Problem saving checked indeces in CheckedListBox

I have several loops in my program that cycle through various CheckedListBoxes and store the checked indeces for each in a single string. This string and a number of other values from oDVD are saved in an XML file later. Here's on of the loops:

Dim Index as Integer
For Each Index In clbVideo.CheckedIndices
oDVD.VideoFormats &= Index.ToString & "|"
Next

This works fine, except that when I check the XML file, I see that the complete set of checked indeces is added to .VideoFormats twice. For example, if items 1 to 5 are checked the result of this loop should be "0|1|2|3|4", but it comes out as "0|1|2|3|4|0|1|2|3|4" instead. Can anyone explain why this is happening? Thanks.

Comments

  • Every time I've had a similar problem it was because I was not clearing the string variable before the loop. For example if a button were clicked once you get "0|1|2|3|4" twice you get "0|1|2|3|4|0|1|2|3|4" three times you get "0|1|2|3|4|0|1|2|3|4|0|1|2|3|4"

    It's from appending data to the leftover data from the prior loop.

    I could be wrong (not too much detail) but if that's the problem all you have to do is add oDVD.VideoFormats = "" just before the loop. Regardless of whether this is the problem, its good practice to make sure you clear out string before appending to it.

    -Ray

    : I have several loops in my program that cycle through various CheckedListBoxes and store the checked indeces for each in a single string. This string and a number of other values from oDVD are saved in an XML file later. Here's on of the loops:
    :
    : Dim Index as Integer
    : For Each Index In clbVideo.CheckedIndices
    : oDVD.VideoFormats &= Index.ToString & "|"
    : Next
    :
    : This works fine, except that when I check the XML file, I see that the complete set of checked indeces is added to .VideoFormats twice. For example, if items 1 to 5 are checked the result of this loop should be "0|1|2|3|4", but it comes out as "0|1|2|3|4|0|1|2|3|4" instead. Can anyone explain why this is happening? Thanks.
    :
    :

  • Thanks, that did it! I just put a Clear method on the DVD class and called it on the DVD object before updating its properties. In some cases it would already contain information (the user might edit instead of adding an entry). That Clear method solves that problem. Thanks again :)

    : Every time I've had a similar problem it was because I was not clearing the string variable before the loop. For example if a button were clicked once you get "0|1|2|3|4" twice you get "0|1|2|3|4|0|1|2|3|4" three times you get "0|1|2|3|4|0|1|2|3|4|0|1|2|3|4"
    :
    : It's from appending data to the leftover data from the prior loop.
    :
    : I could be wrong (not too much detail) but if that's the problem all you have to do is add oDVD.VideoFormats = "" just before the loop. Regardless of whether this is the problem, its good practice to make sure you clear out string before appending to it.
    :
    : -Ray
    :
    : : I have several loops in my program that cycle through various CheckedListBoxes and store the checked indeces for each in a single string. This string and a number of other values from oDVD are saved in an XML file later. Here's on of the loops:
    : :
    : : Dim Index as Integer
    : : For Each Index In clbVideo.CheckedIndices
    : : oDVD.VideoFormats &= Index.ToString & "|"
    : : Next
    : :
    : : This works fine, except that when I check the XML file, I see that the complete set of checked indeces is added to .VideoFormats twice. For example, if items 1 to 5 are checked the result of this loop should be "0|1|2|3|4", but it comes out as "0|1|2|3|4|0|1|2|3|4" instead. Can anyone explain why this is happening? Thanks.
    : :
    : :
    :
    :

  • Good deal...

    -Ray

    : Thanks, that did it! I just put a Clear method on the DVD class and called it on the DVD object before updating its properties. In some cases it would already contain information (the user might edit instead of adding an entry). That Clear method solves that problem. Thanks again :)
    :
    : : Every time I've had a similar problem it was because I was not clearing the string variable before the loop. For example if a button were clicked once you get "0|1|2|3|4" twice you get "0|1|2|3|4|0|1|2|3|4" three times you get "0|1|2|3|4|0|1|2|3|4|0|1|2|3|4"
    : :
    : : It's from appending data to the leftover data from the prior loop.
    : :
    : : I could be wrong (not too much detail) but if that's the problem all you have to do is add oDVD.VideoFormats = "" just before the loop. Regardless of whether this is the problem, its good practice to make sure you clear out string before appending to it.
    : :
    : : -Ray
    : :
    : : : I have several loops in my program that cycle through various CheckedListBoxes and store the checked indeces for each in a single string. This string and a number of other values from oDVD are saved in an XML file later. Here's on of the loops:
    : : :
    : : : Dim Index as Integer
    : : : For Each Index In clbVideo.CheckedIndices
    : : : oDVD.VideoFormats &= Index.ToString & "|"
    : : : Next
    : : :
    : : : This works fine, except that when I check the XML file, I see that the complete set of checked indeces is added to .VideoFormats twice. For example, if items 1 to 5 are checked the result of this loop should be "0|1|2|3|4", but it comes out as "0|1|2|3|4|0|1|2|3|4" instead. Can anyone explain why this is happening? Thanks.
    : : :
    : : :
    : :
    : :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories