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

时间:2015-03-06 16:49:33

标签: c# drag-and-drop 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);
        }
    }
}

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

1 个答案:

答案 0 :(得分:2)

您只需使用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);
            }
   }