I have a combobox with cities and a listbox for customers.
When I select a city, I display all customers who stay there in my listbox. The problem is that when I select a city and the customers get loaded, and I select the same city again it adds the customers again. Is there a way where I can avoid repeating items in a listbox?
I hope this is clear, thanks in advance

Satyapriya NayakRank #47 · 31.5K reputationPosted Jul 25, 2012, 3:53 AM EDT
Try this...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("Raj");
listBox1.Items.Add("Raj");
listBox1.Items.Add("Raju");
listBox1.Items.Add("Rahul");
listBox1.Items.Add("Harry");
listBox1.Items.Add("Ben");
listBox1.Items.Add("Ian");
}
private void btn_right_Click(object sender, EventArgs e)
{
if (listBox2.Items.Contains(listBox1.SelectedItem) == true)
{
MessageBox.Show("Item Exits");
}
else
{
listBox2.Items.Add(listBox1.SelectedItem);
int i = 0;
i = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(i);
}
}
private void btnleft_Click(object sender, EventArgs e)
{
listBox1.Items.Add(listBox2.SelectedItem);
int i = 0;
i = listBox2.SelectedIndex;
listBox2.Items.RemoveAt(i);
}
}
}
Thanks
If this post helps you mark it as answer
VulpesRank #55 · 29.2K reputationPosted Jul 25, 2012, 1:00 AM EDT
listBox1.Items.Clear();
Avuya MxoliRank #7,940 · 231 reputationPosted Jul 25, 2012, 1:55 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 25, 2012, 2:20 AM EDT
Avuya MxoliRank #7,940 · 231 reputationPosted Jul 29, 2012, 2:54 AM EDT