Results 1 to 19 of 19

Thread: [RESOLVED] After populating list box, remove item(s)

  1. #1

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    156

    Resolved [RESOLVED] After populating list box, remove item(s)

    I have been out of this stuff for too long.
    With help from this forum, I managed to populate my listbox, now I am trying to remove items based on a prefix "#".
    For testing I entered the exact string of one item.
    The code populates fine, pops up a message box showing the string I want to find but I cant get the item removed from the list box.
    Where is my error?


    Code:
       Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ListBox2.Items.Clear()
            ListBox2.Sorted = True
            Dim folderPath As String
            folderPath = "C:\Myfiles\"
            Dim folder As New IO.DirectoryInfo(folderPath)
            With ListBox2
                .DisplayMember = "Name"
                .ValueMember = "FullName"
                .DataSource = folder.GetDirectories()
            End With
            'remove items with prefix "#"
            Dim strValue As String = "#Archive Old file storage method" 'for testing
            For x As Integer = 0 To ListBox2.Items.Count - 1
                If ListBox2.Items(x).ToString = strValue Then
                    MsgBox(strValue) 'for visual testing: Finds the item no problem
                    ListBox2.Items.Remove(x) 'doesnt remove the item
                End If
            Next
        End Sub

    As always, any help is appreciated.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

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

    Re: After populating list box, remove item(s)

    Hi ,

    try with SelectedItem
    Code:
    ListBox1.Items.Remove(ListBox1.SelectedItem)
    regards
    Chris

  3. #3
    Addicted Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    180

    Re: After populating list box, remove item(s)

    Use ListBox2.Items.RemoveAt(x) as you are iterating over all the lines in the control.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: After populating list box, remove item(s)

    It's bound...
    Code:
            With ListBox2
                .DisplayMember = "Name"
                .ValueMember = "FullName"
                .DataSource = folder.GetDirectories()
            End With
    I don't think you can remove it from the listbox when it's bound to something like that since it doesn't know if you'll also want it removed from the datasource as well or not. Therefore, it needs to be removed from the bound source. Or... shouldn't have been added in the first place.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: After populating list box, remove item(s)

    Also... you shouldn't go forward through a list like that when removing things...
    Code:
            For x As Integer = 0 To ListBox2.Items.Count - 1
                If ListBox2.Items(x).ToString = strValue Then
                    MsgBox(strValue) 'for visual testing: Finds the item no problem
                    ListBox2.Items.Remove(x) 'doesnt remove the item
                End If
            Next
    You'll run into an index out of range error at some point.

    Let's say you have 5 things, indexed 0 -4:
    [0] = One
    [1] = Two
    [2] = Buckle
    [3] = My
    [4] = Shoe

    For a = 0 to list.items.count-1 (4)

    a = 0
    list.items.remove(a)
    everything shuffles down 1
    [0] = Two
    [1] = Buckle
    [2] = My
    [3] = Shoe
    (keep in mind, we're still going to count to 4 though)
    a increments, and becomes 1

    list.items.remove(a)
    [0] = Two
    [1] = My
    [2] = Shoe
    ... wait, we just skipped right over "Two"

    a increments, and becomes 2
    list.items.remove(a)
    [0] = Two
    [1] = My

    a increments, and becomes 3
    -- index out of array bounds error...

    instead, work it backwards:

    [0] = One
    [1] = Two
    [2] = Buckle
    [3] = My
    [4] = Shoe

    For a = list.items.count-1 (4) to 0 step -1

    a= 4
    list.items.remove(a)
    [0] = One
    [1] = Two
    [2] = Buckle
    [3] = My

    a decrements becomes 3
    list.items.remove(a)
    [0] = One
    [1] = Two
    [2] = Buckle

    a decrements becomes 2
    list.items.remove(a)
    [0] = One
    [1] = Two

    a decrements becomes `1
    list.items.remove(a)
    [0] = One

    a decrements becomes 0
    list.items.remove(a)

    -list is now empty, no errors.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Addicted Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    180

    Re: After populating list box, remove item(s)

    Well spotted, that seems logical.

  7. #7

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    156

    Re: After populating list box, remove item(s)

    Quote Originally Posted by techgnome View Post
    It's bound...
    Code:
            With ListBox2
                .DisplayMember = "Name"
                .ValueMember = "FullName"
                .DataSource = folder.GetDirectories()
            End With
    I don't think you can remove it from the listbox when it's bound to something like that since it doesn't know if you'll also want it removed from the datasource as well or not. Therefore, it needs to be removed from the bound source. Or... shouldn't have been added in the first place.

    -tg
    techgnome appears to be correct about the datasource.
    That's the error I get from trying "ListBox2.Items.Remove(ListBox2.SelectedItem)" and "ListBox2.Items.RemoveAt(x)".

    How do I prevent any string with a prefix of "#" from being added?
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

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

    Re: After populating list box, remove item(s)

    Hi

    your Datasource is C:\MyFiles\ and you have Bound this to the Listbox.
    do you delete the File from the Datasource ?

    or am I missing something?

    regards
    Chris

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: After populating list box, remove item(s)

    Quote Originally Posted by sRLS View Post
    How do I prevent any string with a prefix of "#" from being added?
    vb.net Code:
    1. .DataSource = folder.GetDirectories().
    2.                      Where(Function(fi) Not fi.Name.StartsWith("#")).
    3.                      ToArray()

  10. #10

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    156

    Re: After populating list box, remove item(s)

    No, it is not deleted from the datasource. (Or maybe that is what I think, Im not deleting a file if that is your meaning)
    I am attempting to remove it from the listbox list only.

    Wow, do I sound like a noob or what? haha
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

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

    Re: After populating list box, remove item(s)

    well if you don't delete from the Datasource(remember your Datasource is C:\MyFiles)
    then is does not make any sence in what you are doing

    delete from Listbox but not from Datasource? think about it

    regards
    Chris

  12. #12
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: After populating list box, remove item(s)

    Quote Originally Posted by ChrisE View Post
    your Datasource is C:\MyFiles)
    Not quite, his data source is an array of objects that represent the directories in C:\MyFiles ... so to remove it from the listbox, he needs to remove it from the datasource, which is the array... John's posted what I think is the easiest solution... filter out the items that start with "#", convert it to an array, and set that as the datasource.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: After populating list box, remove item(s)

    John's posted what I think is the easiest solution... filter out the items that start with "#", convert it to an array, and set that as the datasource.
    ah I see now!
    have'nt done that much with Files in .Net.
    learn't somthing again

    regards
    Chris

  14. #14
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: After populating list box, remove item(s)

    Well, what he posted has nothing to do with files. It's using a bit of LINQ actually... The WHERE is an extension that allows you to define a function that can filter out items from the array, similar to the WHERE clause of a SQL Statement. It has nothing to do with files. Let's say you had an array full of integers and you want to return all of the odd values...
    Code:
    dim oddVals() as integer = allMyIntegers.Where(function (i) i mod 2 <> 0).ToArray
    What that does is pass everything in the allMyIntegers array through the function where it is then modulo'd 2 and checked if the result is 0, if not, it's returned, and then the whole results are then used to create an array and stored in oddVals.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    156

    Re: After populating list box, remove item(s)

    The only reason to filter or remove an item from the list box was due to particular folders that do not need to be in the list box.
    using datasource seems to automatically highlite the first item in the list box, which in my case populates a text box.
    Trying to avoid that to keep users from being confused.

    This is how I went about this with out data binding(?).


    Code:
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ListBox2.Items.Clear()
            ListBox2.Sorted = True
    
            For Each folder As String In System.IO.Directory.GetDirectories("C:\Myfiles\")
                ListBox2.Items.Add(IO.Path.GetFileName(folder))
            Next
        End Sub
    Thank you to all who helped!
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  16. #16
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: [RESOLVED] After populating list box, remove item(s)

    Wait... so how does that remove directories with a leading # in them? If all you were after was to prevent highlighting an item after setting the datasource, then you should have said so... because that's something else entirely! Your code does the exact samething that setting the datasource does in the first place....

    All this time you asked for X, when you wanted M ...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  17. #17

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    156

    Re: [RESOLVED] After populating list box, remove item(s)

    It doesnt, it does remove the folder (with a "#" sign) from being auto selected.
    It has to do with my amateur coding causing an issue down line. When I figure that out I will try the other examples.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  18. #18
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: [RESOLVED] After populating list box, remove item(s)

    So you never really wanted it removed... you just didn't want it selected by default....
    Listbox.SelectedIndex = -1

    snort

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: [RESOLVED] After populating list box, remove item(s)

    Quote Originally Posted by techgnome View Post
    So you never really wanted it removed... you just didn't want it selected by default....
    Listbox.SelectedIndex = -1

    snort

    -tg
    Amen

    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.

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