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

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.

Perhaps OP should be writing item.Text as in

hisWrite.WriteLine(item.Text)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.