0

perhaps there is some sort of logic error i am missing here but i cannot figure out whats going wrong. I wrote this simple for loop to write the contents of a ComboBox to a txt file. The process works but for some reason it is doing the job twice. I have tried a regular for loop, a for each loop and a while loop and i keep getting the same result. What am i doing wrong here?

Loop

Private Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
        Dim histWrite As New StreamWriter("history.txt", True)

        For Each item As Object In cmbo_history.Items
            histWrite.WriteLine(item)
        Next
        histWrite.Close()
End Sub

and this is always my result (duplicates are not allowed to be entered into the comboBox so this has to be a problem with the loop

test1
test2
test1
test2

Any help would be greatly appreciated. Thanks all

3
Contributors
2
Replies
15
Views
3 Years
Discussion Span
Last Post by Reverend Jim
1

first off, this thread belongs to vb.net forum.
But , Consider following :

histWrite.WriteLine(item)

item is object type and if you print it to the file then it will be inserted as string. So , write this :-

hisWrite.WriteLine(item.ToString())

and this is always my result (duplicates are not allowed to be entered into the comboBox so this has to be a problem with the loop

This is because that you are opening the file in append mode and may you execute the procedure 2 times.so put false instead of true in StreamWriter.
So , write this :

Dim histWrite As New StreamWriter("history.txt", False)

hope this helps you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.