private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {           if(e.Button == System.Windows.Forms.MouseButtons.Left)
               (sender as ListBox).DoDragDrop((sender as ListBox).SelectedItem, DragDropEffects.Move);  
        }
  ListBox listbox = null;
  string s = "";
private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(ListBox)) && e.Data.GetDataPresent(DataFormats.Text))
            {
                listbox = e.Data.GetData(typeof(ListBox)) as ListBox;
                s = e.Data.GetData(DataFormats.Text).ToString();
                e.Effect = DragDropEffects.Move;
            }
        }
private void listBox1_DragDrop(object sender, DragEventArgs e)
        {           
                (sender as ListBox).Items.Add(s);
                listbox.Items.Remove(s);            
        }上面代码,实现listBox1和listBox2的相互拖动项,都注册了上面那些事件,
为什么不成功呢,哪里错了呢?
谢谢!!

解决方案 »

  1.   

    你窗体和listbox的allowdrop 属性都设为true了么?
      

  2.   


    private bool isLeftMouseButtonDown = false;        private void listBox_DragDrop(object sender, DragEventArgs e)
            {
                ListBox destListBox = sender as ListBox;
                if (e.Data.GetDataPresent(typeof(ListBox)))
                {
                    ListBox srcListBox = e.Data.GetData(typeof(ListBox)) as ListBox;
                    destListBox.Items.Add(srcListBox.SelectedItem);
                    srcListBox.Items.Remove(srcListBox.SelectedItem);
                }
            }        private void listBox_DragEnter(object sender, DragEventArgs e)
            {
                e.Effect = e.AllowedEffect;
            }        private void listBox_MouseDown(object sender, MouseEventArgs e)
            {
                isLeftMouseButtonDown = true;
            }        private void listBox_MouseMove(object sender, MouseEventArgs e)
            {
                if (isLeftMouseButtonDown)
                {
                    ListBox lb = sender as ListBox;
                    if (lb.SelectedItem != null)
                    {
                        lb.DoDragDrop(lb, DragDropEffects.Move);
                    }
                }
            }        private void listBox_MouseUp(object sender, MouseEventArgs e)
            {
                isLeftMouseButtonDown = false;
            }试试这个  这个我试过的 可以的  两个listbox都注册一下
      

  3.   

    第一步: 第一步: 窗体上放两个ListBox, (ListBox1 和 ListBox2)
    第二步: 将窗体、ListBox1、ListBox2的 AllowDrop
    第三步:在ListBox1的MouseDown事件里面放代码:int iSelectedIndex = this.listBox1.SelectedIndex;
    listBox1.DoDragDrop(listBox1.Items[iSelectedIndex], DragDropEffects.Move);
    第四步:在ListBox2的DragEnter事件里面放代码:if (e.Data.GetDataPresent("Text"))
    {
      e.Effect= DragDropEffects.Move;
    }第五步:在ListBox2的DragDrop事件里放代码listBox2.Items.Add(e.Data.GetData("Text"));
    listBox1.Items.Remove(e.Data.GetData("Text"));
    另外你需要写点代码在ListBox1里面添加几个测试数据; 完成后编译运行。
      

  4.   

    第二步: 将窗体、ListBox1、ListBox2的 AllowDrop 属性设置为True
    第三步的第二行代码: listBox1.DoDragDrop(listBox1.Items[iSelectedIndex], DragDropEffects.Move);  
    帖子乱了还有问题:QQ: 920851350
      

  5.   

    Jevin8011那方法是可以的,我之前做项目的过程中有一个模块用到过类似的功能,原理和楼上的差不多,楼主可以采用Jevin8011的方法来做,没问题的。
      

  6.   

    我的代码,和Jevin8011的差不多吧,我想知道,我的哪里错了