-
Jan 6th, 2018, 11:35 AM
#1
Thread Starter
Lively Member
[RESOLVED] [help code] exclude items from a listbox with a text file
Hello I START coding and i need help please!
OK i have a listbox1 with names
and a text file with a list of names
and i would like to exclude the names specifics in listbox1 with similar names in text file.
the result will be in text file.
example;
list in listbox:
henri
vincent
david
rocky
paul
list in text file:
vincent
rocky
paul
i want this result in text file
henri
david
thank you for yours answers!
Last edited by danzey; Jan 9th, 2018 at 01:42 PM.
-
Jan 6th, 2018, 11:58 AM
#2
Re: [help code] exclude items from a listbox with a text file
What have you tried so far? A starting place would be reading the documentation for File.ReadAllLines Method
-
Jan 6th, 2018, 11:59 AM
#3
Re: [help code] exclude items from a listbox with a text file
Hi,
what do you mean exactly ? do you want to remove Names that
are more than once in the Listbox ?
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Jan 6th, 2018, 03:28 PM
#4
Thread Starter
Lively Member
Re: [help code] exclude items from a listbox with a text file
not exactly, i want remove the names specific in listbox thank to a text file( in the text file there is the names i want to remove from the listbox)
i hope it clear
thanks for your help.
-
Jan 6th, 2018, 05:00 PM
#5
Re: [help code] exclude items from a listbox with a text file
Originally Posted by danzey
not exactly, i want remove the names specific in listbox thank to a text file( in the text file there is the names i want to remove from the listbox)
i hope it clear
thanks for your help.
It was completely clear to me the first time. Why should I just write your code without you even trying? I gave you a starting point yet you ignored my reply because another post looked like an easy choice saving you writing any code.
What would you actually learn from me writing it for you? I gave you a starting point yet I can bet you have not even looked at it. vbforums.com is not here to write your projects for you. In this case a method.
Make an effort and I will happily keep nudging you in the right direction when you show actual effort. If not I will unsubscribe from this topic.
-
Jan 8th, 2018, 01:34 PM
#6
Thread Starter
Lively Member
Re: [help code] exclude items from a listbox with a text file
OK i am beginner but i am not lazy.
could you tell me if i go in the good path.
Code:
1. record the text file in listbox1
2. remove the items in listbox2 thank to listbox1
Dim li As Object = ListBox1.Items
ListBox2.Items.Remove(li) ;
thank you for your answers !
-
Jan 8th, 2018, 02:35 PM
#7
Re: [help code] exclude items from a listbox with a text file
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
-
Jan 8th, 2018, 06:56 PM
#8
Thread Starter
Lively Member
Re: [help code] exclude items from a listbox with a text file
Originally Posted by dday9
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
Thank you very much
i try it and this code is good but the step 1 are not related with my program. i let me explain to you
the database is fixed ( or stable) in a text.file and i want ,after loading this data (in listbox or else) thank to text file to delete the similars names
from this code, my problem is how to load my database (step 1) then execute the following ( i don t use a button)
Again thank you so much for your patience and your Help
Last edited by danzey; Jan 9th, 2018 at 01:09 AM.
-
Jan 9th, 2018, 05:00 AM
#9
Thread Starter
Lively Member
Re: [help code] exclude items from a listbox with a text file
Originally Posted by danzey
Thank you very much
i try it and this code is good but the step 1 are not related with my program. i let me explain to you
the database is fixed ( or stable) in a text.file and i want ,after loading this data (in listbox or else) thank to text file to delete the similars names
from this code, my problem is how to load my database (step 1) then execute the following ( i don t use a button)
Again thank you so much for your patience and your Help
someone can help me , please?
-
Jan 9th, 2018, 09:53 AM
#10
Re: [help code] exclude items from a listbox with a text file
Hi,
what code have you got sofar ?
your concept does'nt really make sence, if you have a Textfile with names why don't you
just open it and Add/Edit/Delete
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Jan 9th, 2018, 10:36 AM
#11
Thread Starter
Lively Member
Re: [help code] exclude items from a listbox with a text file
Originally Posted by ChrisE
Hi,
what code have you got sofar ?
your concept does'nt really make sence, if you have a Textfile with names why don't you
just open it and Add/Edit/Delete
regards
Chris
yes but i want to make it automatically when i open my form.
the reason is to have a another file ( with others variables)
indeed, obtain a list name wihout, for example , the "qualified ones" because it is a program for sport
ok , i hope someone have the solution. thank you
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|