I'm trying to count the number of an item in a text file, by counting each instance the item was entered into the file earlier on in the program.

I already have the text read from the file and in a text box. The problem is that my current code was just counting the characters in the textbox and not the number of times my desired word was in the file.

For Each desiredword As String In txtContentofFile.Text
intdesiredword = intdesiredword + 1
txtdesiredwordcount.Text = intdesiredword
Next

This counts the characters in the textbox instead of counting the number of desired words. I tried repeatedly before asking help and searched extensively, but I just don't understand what's wrong with my code. Please help :)

Private Function SearchWords(ByVal allWords As List(Of String), ByVal wordsToSearch() As String) As Dictionary(Of String, List(Of Integer))
Dim dResult As New Dictionary(Of String, List(Of Integer))()
Dim i As Integer = 0
For Each s As String In wordsToSearch
dResult.Add(s, New List(Of Integer))
While i >= 0 AndAlso i < allWords.Count
i = allWords.IndexOf(s, i)
If i >= 0 Then dResult(s).Add(i)
i += 1
End While
Next
Return dResult
End Function

You will have not only the number of occurances, but the index positions in the file, grouped easily in a Dictionary.

Thank you for such a detailed answer! I'm clearly a lot worse at vb.net than you so it'll take me a while to actually get something based off that working in my own roundabout code. How do I take the answer from this and make it actually display in a textbox? txtnumberofdesired1.Text = dResult didn't work for me
–
Kieran O' mahonyMar 20 '13 at 13:33

@KieranO'mahony Thanks a lot. Take a look at the use at the first part, its a functional example. Basically, you call the function passing to it the words in where to look at, and the words that you are going to search, and it returns a Dictionary with the results of each word. The use is: TextBox1.Text = dResult("myWord").Count
–
SysDragonMar 20 '13 at 14:39