1
votes
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DragDrop
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == )
            listBox2.Items.Add(listBox1.SelectedItem);
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);


        }
        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("Orange");
            listBox1.Items.Add("Pineapple");
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            listBox1.DoDragDrop(listBox1.SelectedItem, DragDropEffects.Move);
        }

        private void listBox2_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effect = DragDropEffects.Move;
            }
        }

        private void listBox2_DragDrop(object sender, DragEventArgs e)
        {
            string strText = "";

            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                strText = (string) e.Data.GetData(DataFormats.Text);
            }

            listBox2.Items.Add(strText);
            listBox1.Items.Remove(strText);
        }
    }
}

I'm trying to create an application that takes item from listbox and adds them to another by clicking a button. The application adds items to the listbox fine but i'm trying to avoid duplicates. When one item is added i want it to make sure that the item isn's already there. The item is removed when the button is clicked so the duplicate issue is avoided there but when the item is added via drag and drop it doesn't check for duplicates.

1

1 Answers

3
votes

You can simply check for item existence using Items.Contains() method.

So you need to use something like following

listBox2.Items.Contains(listBox1.SelectedItem)

The updated code would look like below,

 private void button1_Click(object sender, EventArgs e)
 {
      if (listBox1.SelectedItem != null)
      {
          // Check for existence and add if it's a new item 
          if (!listBox2.Items.Contains(listBox1.SelectedItem))
          {
                    listBox2.Items.Add(listBox1.SelectedItem);
                    listBox1.Items.RemoveAt(listBox1.SelectedIndex); 
           }
       }
  }

  private void Form1_Load(object sender, EventArgs e)
  {
            listBox1.Items.Add("Orange");
            listBox1.Items.Add("Pineapple");
            listBox1.Items.Add("Pineapple"); // <= Simply add this repetitive item
  }

  private void listBox1_MouseDown(object sender, MouseEventArgs e)
  {
         if (listBox1.SelectedItem != null)
         {
                listBox1.DoDragDrop(listBox1.SelectedItem, DragDropEffects.Move);
            }
  }

  private void listBox2_DragEnter(object sender, DragEventArgs e)
  {
         if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effect = DragDropEffects.Move;
            }
  }

  private void listBox2_DragDrop(object sender, DragEventArgs e)
  {
            string strText = "";

            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                strText = (string)e.Data.GetData(DataFormats.Text);
            }

            // Check for existence and add if it's a new item 
            if (!listBox2.Items.Contains(listBox1.SelectedItem))
            {
                listBox2.Items.Add(strText);
                listBox1.Items.Remove(strText);
            }
   }