Assuming that the text file already contains some names and the user inputs names into a ListBox, you can do the following:
Step 1. Allow the user to insert names to a List(Of String) and then bind the List to the ListBox.
Step 2. Once the user's ready to only keep the unique values, add all the names that appear in the text file to the List(Of String)
Step 3. Select each value from the List(Of String) that only appears once
Step 4. Write the values from the List(Of String) back to the text file
Here is a quick example:
Code:Public Class Form1 Private names As New List(Of String) Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load 'bind the ListBox ListBoxNames.DataSource = names End Sub Private Sub ButtonAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonAdd.Click 'prompt for a name Dim name As String = InputBox("Please enter a name.") If Not String.IsNullOrWhitespace(name) Then 'add the name to the List(Of String) names.Add(name) End If End Sub Private Sub ButtonRewrite_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonRewrite.Click 'add the names from the text to the collection names.AddRange(IO.File.ReadAllLines("path.txt")) 'get only the unique names from the collection by only keeping the names that appear once names = (From name As String In names Where names.ToArray().Count(Function(n) n = name) = 1).ToList() 'rewrite the text file IO.File.WriteAllLines("path.txt", names.ToArray()) End Sub End Class




Reply With Quote
