阻止列表框将重复项添加到其他列表框

Stop listbox from adding duplicates to other listbox

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

我正在尝试创建一个应用程序,它从列表框中获取项目并通过单击按钮将它们添加到另一个列表框中。该应用程序可以很好地将项目添加到列表框中,但我正在努力避免重复。添加一个项目时,我希望它确保该项目已经存在。单击按钮时项目将被删除,因此避免了重复问题,但是当通过拖放添加项目时,它不会检查重复项。

您可以使用 Items.Contains() 方法简单地检查项目是否存在。

所以你需要使用类似下面的东西

listBox2.Items.Contains(listBox1.SelectedItem)

更新后的代码如下所示,

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