Results 1 to 11 of 11

Thread: [RESOLVED] [help code] exclude items from a listbox with a text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Resolved [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.

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    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

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    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.

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: [help code] exclude items from a listbox with a text file

    Quote Originally Posted by danzey View Post
    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    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 !

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [help code] exclude items from a listbox with a text file

    Quote Originally Posted by dday9 View Post
    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.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [help code] exclude items from a listbox with a text file

    Quote Originally Posted by danzey View Post
    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?

  10. #10
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    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.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [help code] exclude items from a listbox with a text file

    Quote Originally Posted by ChrisE View Post
    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
  •  



Click Here to Expand Forum to Full Width