Listbox之间互相拖拽功能

新建一个Winform。拖两个Listbox,都改allowDrag为true。

复制以下代码,覆盖。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace listbox互相拖拽
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private ListBox lbSource = null;//拖拽数据来源
#region ListBox1拖拽
//首先执行1.当鼠标被按下时候发生
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
int index = listBox1.SelectedIndex;//为-1表示没选中
if (index == -1)
{
return;
}
string s = ((ListBox)sender).Items[index].ToString();//通过index索引找到的项,其实还是那个选中项
lbSource = listBox1;
// DragDropEffects 指定拖放操作的可能效果
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.Move);//2.开始拖拽操作,s为要拖拽的数据:当执行这句话时候开始拖拽,然后系统会转到drag事件里,顺序:dragEnter-->dragDragDrop,完成后会调回。如果在下一句打断点会卡死
if (dde1 == DragDropEffects.Move)//如果移动成功
{
((ListBox)sender).Items.RemoveAt(index);//是把自己位置的删除掉
}

}

//3.在用鼠标将某项拖拽到该控件的工作区时发生
private void listBox2_DragEnter(object sender, DragEventArgs e)
{
//GetDataPresent()确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。
if (e.Data.GetDataPresent(DataFormats.Text) && (lbSource != listBox2))//是文本格式,并且拖动源不是当前listbox。
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}

//4拖拽操作完成时发生
private void listBox2_DragDrop(object sender, DragEventArgs e)
{
//GetDataPresent()确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
listBox2.Items.Add(e.Data.GetData(DataFormats.StringFormat).ToString());
}
}
#endregion

#region ListBox2拖拽
//首先执行1.当鼠标被按下时候发生
private void listBox2_MouseDown(object sender, MouseEventArgs e)
{
int index = listBox2.SelectedIndex;//为-1表示没选中
if (index == -1)
{
return;
}
string s = ((ListBox)sender).Items[index].ToString();//通过index索引找到的项,其实还是那个选中项
lbSource = listBox2;
// DragDropEffects 指定拖放操作的可能效果
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.Move);//2.开始拖拽操作,s为要拖拽的数据:当执行这句话时候开始拖拽,然后系统会转到drag事件里,顺序:dragEnter-->dragDragDrop,完成后会调回。如果在下一句打断点会卡死
if (dde1 == DragDropEffects.Move)//如果移动成功
{
((ListBox)sender).Items.RemoveAt(index);//是把自己位置的删除掉
}

}

//3.在用鼠标将某项拖拽到该控件的工作区时发生
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
//GetDataPresent()确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。
if (e.Data.GetDataPresent(DataFormats.Text) && (lbSource != listBox1))//是文本格式,并且拖动源不是当前listbox。
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}

//4拖拽操作完成时发生
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
//GetDataPresent()确定此实例中存储的数据是否与指定的格式关联,或是否可以转换成指定的格式。
if (e.Data.GetDataPresent(DataFormats.StringFormat))
{
listBox1.Items.Add(e.Data.GetData(DataFormats.StringFormat).ToString());
}
}
#endregion
}
}



 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值