Results 1 to 3 of 3

Thread: [2005] count the same items in a listbox

  1. #1

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    [2005] count the same items in a listbox

    Hi All,

    What I'm trying to do is to populate a listbox with all the words in a richtextbox

    Code:
    Dim str As String = RichtextBox1.Text
    Listbox1.Items.Add(str)

    This works, but it also add's the same words(items).

    How can I count the same words(items).
    For example:

    I like VB.Net very much.
    But sometimes VB.Net doesn't like me.

    The listbox should give me:

    I (1)
    like (2)
    VB.Net (2)
    etc.....

    That's what I have so far:

    Code:
    Dim element As String = Richtextbox1.Text
        If Not ListBox1.Items.Contains(element) Then
            ListBox.Items.Add(element)
    else
            ...... count the same words
        End If
    How can I do that?

    Thanks in advance,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: [2005] count the same items in a listbox

    vb Code:
    1. Dim i As Integer = 0
    2. For Each Str As String In ListBox1.Items
    3.     If Str = "what ever string here" Then
    4.         i = i + 1
    5.     End If
    6. Next
    7. MsgBox(i.ToString())

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] count the same items in a listbox

    Use a Dictionary(Of String, Integer). The words are the keys and the counts are the values.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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