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?!
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)
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):
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.
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):
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
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.
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
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.
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]