拖放到列表框

时间:2014-07-15 17:54:15

标签: c# drag-and-drop listbox

我想将列表框项目拖到另一个列表框中。 我想剪切商品,而不是复制。

如果我将项目拖到相同的列表框,则项目会在此列表框中复制。

我该如何预防?

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

            this.listBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.listBox1_DragEnter);
            this.listBox2.DragOver += new System.Windows.Forms.DragEventHandler(this.listBox2_DragOver);
            this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
            this.listBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox2_MouseDown);
            this.listBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox2_DragDrop);
            this.listBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox1_DragDrop);
        }

        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void listBox2_DragOver(object sender, DragEventArgs e)
        {
            if (e.KeyState == 1)
            {
                e.Effect = DragDropEffects.Move;
            }
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Point sonnokta = new Point(e.X, e.Y);
            int item_index = listBox1.IndexFromPoint(sonnokta);
            if (e.Button == MouseButtons.Left)
            {
                listBox1.DoDragDrop(listBox1.Items[item_index], DragDropEffects.Move);
            }
        }

        private void listBox2_MouseDown(object sender, MouseEventArgs e)
        {
            Point sonnokta = new Point(e.X, e.Y);
            int item_index = listBox2.IndexFromPoint(sonnokta);
            if (e.Button == MouseButtons.Left)
            {
                listBox2.DoDragDrop(listBox2.Items[item_index], DragDropEffects.Move);
            }
        }

        private void listBox2_DragDrop(object sender, DragEventArgs e)
        {
            listBox2.Items.Add(e.Data.GetData(DataFormats.Text));
            listBox1.Items.Remove(e.Data.GetData(DataFormats.Text));
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            listBox1.Items.Add(e.Data.GetData(DataFormats.Text));
            listBox2.Items.Remove(e.Data.GetData(DataFormats.Text));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

只需检查当前ListBox中是否已存在该项:

private void listBox2_DragDrop(object sender, DragEventArgs e) {
  string dragItem = e.Data.GetData(DataFormats.Text) as string;
  if (dragItem != null && !listBox2.Items.Contains(dragItem)) {
    listBox2.Items.Add(dragItem);
    listBox1.Items.Remove(dragItem);
  }
}

private void listBox1_DragDrop(object sender, DragEventArgs e) {
  string dragItem = e.Data.GetData(DataFormats.Text) as string;
  if (dragItem != null && !listBox1.Items.Contains(dragItem)) {
    listBox1.Items.Add(dragItem);
    listBox2.Items.Remove(dragItem);
  }
}