Count Number of items in listbox that have an underscore
Hello,
I need to count the number of objects in listbox1 that have an underscore "_" at the beginning of the file. The underscore will only ever be at the beginning of the filename.
Current Code:
Code:
Dim streamer As IO.StreamReader
Dim directory1 = "C:\Server Media\Folder"
Dim MediaLocation = "C:\Server Media\Folder\"
Dim CopyFormat = "C:\Server Media\Folder\" + "_ "
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim files() As System.IO.FileInfo
Dim dirinfo1 As New System.IO.DirectoryInfo(directory1)
files = dirinfo1.GetFiles("*", IO.SearchOption.AllDirectories)
For Each File In files
ListBox1.Items.Add(File)
Next
Label2.Text = ListBox1.Items.Count
End Sub
Also what would be helpful is having a textbox that would replace the Underscore. As in whatever the user places in the textbook would be placed at the beginning of the file when it is copied. But I keep getting an error when I try to implement that feature.
Code:
Dim CopyFormat = "C:\Server Media\Folder\" + "_ "
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
For Each o As Object In ListBox1.SelectedItems
My.Computer.FileSystem.CopyFile(MediaLocation & o.ToString, CopyFormat & o.ToString)
Next
ListBox1.Items.Clear()
ListBox1.Refresh()
Dim files() As System.IO.FileInfo
Dim dirinfo1 As New System.IO.DirectoryInfo(directory1)
files = dirinfo1.GetFiles("*", IO.SearchOption.AllDirectories)
For Each File In files
ListBox1.Items.Add(File)
Next
Label2.Text = ListBox1.Items.Count
End Sub
Re: Count Number of items in listbox that have an underscore
Code:
Private Function GetUnderscoreCount(lstBox As ListBox) As Integer
Return (From item In lstBox.Items Where item.ToString().Contains("_")).Count()
End Function
Just pass the list box you want in the params and it'll do the magic for you.
Re: Count Number of items in listbox that have an underscore
So how do I get that code to display into label3?
Code:
Private Function GetUnderscoreCount(lstBox As ListBox) As Integer
Return (From item In ListBox1.Items Where item.ToString().Contains("_")).Count()
End Function
Re: Count Number of items in listbox that have an underscore
Label3.Text = GetUnderscoreCount(ListBox1)
Re: Count Number of items in listbox that have an underscore
So it should look like this? Because this is not working for me..
Code:
Private Function GetUnderscoreCount(lstBox As ListBox) As Integer
Return (From item In ListBox1.Items Where item.ToString().Contains("_")).Count()
Label3.Text = GetUnderscoreCount(ListBox1)
End Function
Does this as is change automatically if the list changes or do I need to make this work with a button as well?
Re: Count Number of items in listbox that have an underscore
Read and learn how a function works but this is the core basics of programming. Learn how it works and apply it here.
Re: Count Number of items in listbox that have an underscore
Well I have looked all over the internet on how to make this work before posting here so if your not trying to help don't post!
Re: Count Number of items in listbox that have an underscore
What do you mean I'm not trying to help? I've literally spent my time coding a working solution for you. I even told you how to make it work. What else do you want me to do?
I believe in helping others, but others should also help themselves. If you had learnt what/how a function works you'd easily spot the mistake. I'll leave you to figure it out yourself so you can learn something out of it.
Re: Count Number of items in listbox that have an underscore
yeah that is what I'm saying is you have told me to go search somewhere for how a private function works. I have done that. I'm just not good at this so a mistake is not going to JUMP out at me like it would with someone who has done this for a while.
Examples I have seen are using string and not integer, that is not causing the error is it?
Re: Count Number of items in listbox that have an underscore
A function returns a value, that's it's sole purpose.
So, this function.
Code:
Private Function GetUnderscoreCount(lstBox As ListBox) As Integer
Return (From item In ListBox1.Items Where item.ToString().Contains("_")).Count()
End Function
Will return a Count, that's all. You don't need to modify that code.
Now just call that function and display the count returned from it like so.
Code:
Label3.Text = GetUnderscoreCount(ListBox1)
Make sure you call it OUTSIDE the function and not inside like you did before.
Re: Count Number of items in listbox that have an underscore
When the function encounters a return statement, execution of the code return to the caller. You need to use it this way...
Code:
Private sub CallingCode
dim count as integer = GetUnderscoreCount(someListBox)
Label3.Text = count.Tostring
End sub
Private Function GetUnderscoreCount(lstBox As ListBox) As Integer
Return (From item In ListBox1.Items Where item.ToString().Contains("_")).Count()
End Function
And yea, sometimes giving someone some code, then telling them to go figure out how to use it not so helpful.
Re: Count Number of items in listbox that have an underscore
You don't include the label.text assignment line in the function like that.
If you want to display the count on clicking a button, then you assign the label.text in the button's click event. If you want to display the count after adding items, you assign the label.text after your item adding code...
Re: Count Number of items in listbox that have an underscore
Quote:
Originally Posted by
kebo
And yea, sometimes giving someone some code, then telling them to go figure out how to use it not so helpful.
I showed OP how to use the function with a demonstration. Is not my fault he doesn't know how a function works and I've helped enough. I'm sure vbforums is not a spoonfeeding club. Is good to encourage people to learn rather than holding their hand.
Re: Count Number of items in listbox that have an underscore
BTW Toph... Your code will only work if the listbox contains plain strings. If it is bound or uses custom listitems it won't work.
Re: Count Number of items in listbox that have an underscore
Quote:
Is not my fault he doesn't know how a function works
No, but it is your fault for spending more time telling s/he to go look it up rather than just showing how to use it which is what you ended up doing anyway. Next time just answer or don't post man, its that simple. And you really shouldn't be faulting someone for not knowing something you think they should know.
Done.
Re: Count Number of items in listbox that have an underscore
Quote:
Originally Posted by
kebo
No, but it is your fault for spending more time telling s/he to go look it up rather than just showing how to use it which is what you ended up doing anyway. Next time just answer or don't post man, its that simple. And you really shouldn't be faulting someone for not knowing something you think they should know.
Done.
I told him how to use it in the beginning.
http://prntscr.com/7j9apo
Re: Count Number of items in listbox that have an underscore
Code:
Private Function GetUnderscoreCount(ByVal lstBox As ListBox) As Integer
Return lstBox.Items.Cast(Of Object).Where(Function(o) lstBox.GetItemText(o).StartsWith("_")).Count()
End Function
Re: Count Number of items in listbox that have an underscore
Quote:
Originally Posted by
Toph
A function returns a value, that's it's sole purpose.
So, this function.
Code:
Private Function GetUnderscoreCount(lstBox As ListBox) As Integer
Return (From item In ListBox1.Items Where item.ToString().Contains("_")).Count()
End Function
Will return a Count, that's all. You don't need to modify that code.
Now just call that function and display the count returned from it like so.
Code:
Label3.Text = GetUnderscoreCount(ListBox1)
Make sure you call it OUTSIDE the function and not inside like you did before.
I was able to get this to work correctly thanks.