-
Aug 22nd, 2017, 09:48 AM
#1
Thread Starter
Addicted Member
[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!
-
Aug 22nd, 2017, 10:09 AM
#2
Re: After populating list box, remove item(s)
Hi ,
try with SelectedItem
Code:
ListBox1.Items.Remove(ListBox1.SelectedItem)
regards
Chris
-
Aug 22nd, 2017, 10:09 AM
#3
Addicted Member
Re: After populating list box, remove item(s)
Use ListBox2.Items.RemoveAt(x) as you are iterating over all the lines in the control.
-
Aug 22nd, 2017, 10:13 AM
#4
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
-
Aug 22nd, 2017, 10:20 AM
#5
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
-
Aug 22nd, 2017, 10:22 AM
#6
Addicted Member
Re: After populating list box, remove item(s)
Well spotted, that seems logical.
-
Aug 22nd, 2017, 10:29 AM
#7
Thread Starter
Addicted Member
Re: After populating list box, remove item(s)
Originally Posted by techgnome
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!
-
Aug 22nd, 2017, 11:02 AM
#8
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
-
Aug 22nd, 2017, 11:09 AM
#9
Re: After populating list box, remove item(s)
Originally Posted by sRLS
How do I prevent any string with a prefix of "#" from being added?
vb.net Code:
.DataSource = folder.GetDirectories().
Where(Function(fi) Not fi.Name.StartsWith("#")).
ToArray()
-
Aug 22nd, 2017, 11:10 AM
#10
Thread Starter
Addicted Member
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!
-
Aug 22nd, 2017, 11:36 AM
#11
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
-
Aug 22nd, 2017, 12:16 PM
#12
Re: After populating list box, remove item(s)
Originally Posted by ChrisE
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
-
Aug 22nd, 2017, 12:30 PM
#13
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
-
Aug 22nd, 2017, 12:53 PM
#14
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
-
Aug 23rd, 2017, 12:28 PM
#15
Thread Starter
Addicted Member
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!
-
Aug 23rd, 2017, 12:59 PM
#16
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
-
Aug 23rd, 2017, 01:23 PM
#17
Thread Starter
Addicted Member
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!
-
Aug 23rd, 2017, 01:56 PM
#18
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
-
Aug 23rd, 2017, 02:25 PM
#19
Re: [RESOLVED] After populating list box, remove item(s)
Originally Posted by techgnome
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|