Click here to Skip to main content
15,919,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,



I have two listboxs so when i select one item and click button then the items from listbox1 will move to listbox2.It is done but if there are similar items in listbox1 after moving one item it should not allow to move the duplicate items rather it will show a message item already available.How to do it.


My Code below


C#
private void btnright_Click(object sender, EventArgs e)
        {

            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);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("a");
            listBox1.Items.Add("a");
            listBox1.Items.Add("b");
            listBox1.Items.Add("c");
            listBox1.Items.Add("d");
            listBox1.Items.Add("e");
            listBox1.Items.Add("f");


        }




Thanks
Posted
Updated 3-Dec-11 0:54am
v2

Your question makes no real sense based on your sample. You say an object can't be added to the 2nd listbox if it's already IN the listbox, yet, more than one of that item IS allowed in the first listbox. This is nonsense. Because you're implying that the two instances of "a" are the same in one listbox, but DIFFERENT in the other.
 
Share this answer
 
You can use the following code on btnRight
C#
if (listBox2.Items.Contains(listBox1.SelectedItem)== true)
          {
              MessageBox.Show("Duplicate Value");
          }
          else
          {
              listBox2.Items.Add(listBox1.SelectedItem);
              i = 0;
              i = listBox1.SelectedIndex;
              listBox1.Items.RemoveAt(i);

          }

I hope it will help you. :)

-MKB
 
Share this answer
 
Comments
3796068 4-Dec-11 5:39am    
Thanks Friend

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900