inserting item into list box
-
hi i m inserting some vlues into a listbox but now i want to restrict the duplication of entries in the list box private void lblpush_Click(object sender, EventArgs e) { for (int i = 0; i < tbxnumber.Text.Length - 1; i++) { if (list1.Items[i] == tbxnumber.Text[i]) return; object list = tbxnumber.Text[i]; list1.Items.Add(list); } } but it does not work
-
hi i m inserting some vlues into a listbox but now i want to restrict the duplication of entries in the list box private void lblpush_Click(object sender, EventArgs e) { for (int i = 0; i < tbxnumber.Text.Length - 1; i++) { if (list1.Items[i] == tbxnumber.Text[i]) return; object list = tbxnumber.Text[i]; list1.Items.Add(list); } } but it does not work
Here you go
private void InsertListBoxItem(string NewItem)
{
if (!listBox1.Items.Contains(NewItem))
{
listBox1.Items.Add(NewItem);
}
}Hope it helps :)
Harvey Saayman - South Africa Software Developer .Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111 -
hi i m inserting some vlues into a listbox but now i want to restrict the duplication of entries in the list box private void lblpush_Click(object sender, EventArgs e) { for (int i = 0; i < tbxnumber.Text.Length - 1; i++) { if (list1.Items[i] == tbxnumber.Text[i]) return; object list = tbxnumber.Text[i]; list1.Items.Add(list); } } but it does not work
No, it won't. Problems: 1) You are looping round exactly as many times as there are characters in the textbox, rather than the number of times there are items in the list. 2) You are comparing the list item with a single character. Try: 1) using the codeblock widget to preserve your formatting when you post code fragments - it make it easier to read, and thus understand. 2) using foreach(ListBoxItem lbi in list1) instead of a for loop. 3) using ListBox.Contains instead of the loop at all!
All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!