1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

VB: string counter help?

Discussion in 'Programming' started by ShadyLady, Mar 23, 2012.

  1. #1
    I'm working on a project with Visual Studio 2008.

    I need help doing this...

    1. User adds a string (text) to combo box
    2. the text they typed gets added to a listbox
    3. if the listbox contains TWO or more of the same string then add that string to the second listbox (but only once).

    I know how to create a hit counter for a button. But don't know how i would go about doing it for the different text that the user types.

    HELP?! :)
     
    Solved! View solution.
    ShadyLady, Mar 23, 2012 IP
  2. ShadyLady

    ShadyLady Active Member

    Messages:
    775
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    EXAMPLE
    1: user types HELLO. hello gets added to listbox1
    2. user types HELLO. hello gets added to listbox1
    3. user types GOODBYE. goodbye gets added to listbox1
    4. HELLO gets added to listbox2 (as it occurs more than once in listbox1)
     
    ShadyLady, Mar 23, 2012 IP
  3. #3
    I only have VB 2010, so this might not work in VB 2008, but you probably get the idea.
    
            Dim count As Integer = 0
    
            For Each item In ListBox1.Items
                If item = TextBox1.Text Then
                    count += 1
                End If
            Next
    
            If count > 1 Then
                ListBox2.Items.Add(TextBox1.Text)
            End If
            ListBox1.Items.Add(TextBox1.Text)
    
    Code (markup):
     
    Arttu, Mar 24, 2012 IP
  4. ShadyLady

    ShadyLady Active Member

    Messages:
    775
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #4
    That should work in VB '08.
    Thank you. I shall give that code a try later on and see if it works :) I'll get back to you once I've done that.
     
    ShadyLady, Mar 24, 2012 IP
  5. ShadyLady

    ShadyLady Active Member

    Messages:
    775
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #5
    hmm afraid that code doesn't do anything :(
    It isn't adding the duplicated string into listbox 2
     
    ShadyLady, Mar 24, 2012 IP
  6. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #6
    Are you sure you used it right?

    Try this:
    1 button
    1 textbox
    2 listboxes
    
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim count As Integer = 0
    
            For Each item In ListBox1.Items
                If item = TextBox1.Text Then
                    count += 1
                End If
            Next
    
            If count > 1 Then
                ListBox2.Items.Add(TextBox1.Text)
            End If
            ListBox1.Items.Add(TextBox1.Text)
        End Sub
    End Class
    
    Code (markup):
     
    Arttu, Mar 25, 2012 IP
  7. ShadyLady

    ShadyLady Active Member

    Messages:
    775
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #7
    I'm pretty sure I did. Renamed some things though to work with my code

    But i'll create a completely new project and try it. Hope it will work then i'll be able to understand where I went wrong. :)

    EDIT: yay that worked!! Adds string to listbox 2 if the text is repeated three times...

    Just need to figure out where i went wrong. I might've placed the code in the wrong place lol

    I should get it sorted now hopefully. Thanks alot for your help :)
     
    Last edited: Mar 25, 2012
    ShadyLady, Mar 25, 2012 IP
  8. ShadyLady

    ShadyLady Active Member

    Messages:
    775
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #8
    One last question.

    I got it working (thank very much) but I want to add 'timeoflastclick' after the text that the user types.
    I used the code:

    But then the duplicated items do not get added to listbox 2. But without the '& timeoflastclick' it works just fine. What can i change/add to make it work again but keeping the time added in listbox 1.
     
    ShadyLady, Mar 25, 2012 IP
  9. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #9
    What kind of time format are you going to use?
     
    Arttu, Mar 26, 2012 IP
  10. ShadyLady

    ShadyLady Active Member

    Messages:
    775
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #10
    well i've set the timeoflastclick to be 'date.now()'

    so it shows string and then date & time e.g. text 23:40 26.03.2012
     
    ShadyLady, Mar 26, 2012 IP
  11. Arttu

    Arttu Member

    Messages:
    139
    Likes Received:
    2
    Best Answers:
    8
    Trophy Points:
    40
    #11
    The problem is that date.now() gives me something like this: 27/3/2012 1:00:00 PM, so unless you are making the program for personal use only, you have to come up with a solution that works with all kinds of time formats.

    Here are some possible solutions:
    -Use a listview instead of a listbox and put the time in a separated comlumn.
    -Add some character eg. comma between the text and the time then split the string and check if the first part is the same as the combobox's text.
    - Use InStr(item, ComboBox1.Text) instead of if item = ComboBox1.Text, this wont work if the combox contains both "coffee" and "coffee cup".
    - Force the program to use some specific time format and use regex.
     
    Last edited: Mar 27, 2012
    Arttu, Mar 27, 2012 IP
  12. ShadyLady

    ShadyLady Active Member

    Messages:
    775
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #12
    ah I see.
    I shall have a play about and get it sorted.

    Thanks for your help :)
     
    ShadyLady, Mar 27, 2012 IP
  13. Scarrfo

    Scarrfo Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    you can use
    [TABLE]
    [TR]
    [TD="class: votecell"]
    [TABLE]
    [TR]
    [TD="class: votecell"]

    [/TD]
    [TD="class: answercell"]


    DateTime.Now.ToUniversalTime()



    DateTime.UtcNow

    to provide a standardized time


    [/TD]
    [/TR]
    [/TABLE]


    [/TD]
    [/TR]
    [/TABLE]
     
    Scarrfo, Mar 28, 2012 IP
  14. ShadyLady

    ShadyLady Active Member

    Messages:
    775
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #14
    I'll try that thanks :)
     
    ShadyLady, Mar 30, 2012 IP