Dim wri As New IO.StreamWriter("e:\test.txt", True)
        wri.WriteLine(ListBox1.SelectedItem, true)

        wri.Close())

etc in llistbox1 items , "1" "f1" "f2"

when i click for example f1 that code will save it and when i want you save the same item it wit will save it in other line and in test.text file looks like this

f1
f1
f1
f1
f1
i don't want that
i want to only save once ,not repeatedly save that item in each line
thanks for helping me :)

Is your code wrapped in a loop somewhere?


Can you please post a larger portion of the code so we can analyze it?

see this code , its check if the item is or not exist in txt file

Dim hash As List(Of String) = New List(Of String)(System.IO.File.ReadAllLines("E:\test.txt"))


If Not hash.Contains(ListBox1.SelectedItem.ToString()) Then
    Dim w As New IO.StreamWriter("E:\test.txt", True)
    w.WriteLine(ListBox1.SelectedItem, True)
    w.Close()
Else
    'Item is already in text file
End If

can you fix it ?

Also, see if this helps.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.AddRange(New String() {"1", "f1", "f2"})
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Static selCoolItem As String '// stores the .SelectedItem to verify against the previously .SelectedItem.
        With ListBox1
            If Not .SelectedIndex = -1 Then '// if itm selected.
                If Not selCoolItem = .SelectedItem Then '// check if same item as previously .SelectedItem, if one selected.
                    selCoolItem = .SelectedItem '// store .SelectedItem in String.
                    Dim w As New IO.StreamWriter("c:\test.txt", True) : w.WriteLine(selCoolItem) : w.Close() '// append to File.
                End If
            End If
        End With
    End Sub
End Class

If you don't want the same item to be added to the file, load File in an Array, loop thru all lines and if item found Then Exit the entire Sub with "Exit Sub". If not found, have the code to append to file just underneath your For/Next loop.

commented: ITS REALLY WORKED :D THANKS YOU SO MUCH +1

codeorder
this is one problem with this code
when you etc
select item "number 1" and click save
and select item "number 2" and save it again and so on .. ( when you save these items repeatedly fast)
in txt file you have items like this

number 2
number 1
number 2
number 1
number 2
number 1

Issue solved since thread solved? or do you still need support on this?

yes it want support on this , or i must create a new topic for that bug i found ? or i click Mark this Thread as Unsolved

Why don't you just set the IO.StreamWriter to not Append.

>>yes it want support on this
See if this helps.:)

Imports System.IO
Public Class Form1
    Private myFile As String = "c:\test.txt"
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.AddRange(New String() {"1", "f1", "f2"})
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Static selCoolItem As String '// stores the .SelectedItem to verify against the previously .SelectedItem.
        With ListBox1
            If Not .SelectedIndex = -1 Then '// if itm selected.
                If Not selCoolItem = .SelectedItem Then '// check if same item as previously .SelectedItem, if one selected.
                    selCoolItem = .SelectedItem '// store .SelectedItem in String.
                    For Each itm As String In File.ReadAllLines(myFile) '// read File.Lines and...
                        If itm = selCoolItem Then Return '// ...Exit Sub w/less typing. ;)
                    Next
                    Dim w As New IO.StreamWriter(myFile, True) : w.WriteLine(selCoolItem) : w.Close() '// append to File.
                End If
            End If
        End With
    End Sub
End Class
commented: Oh god :) . this is worked perfectly . THANKS YOU SO MUCH AGAIN +1
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.