本文介绍了如何在C#中的2个列表框之间拖动多个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以将一个项目从listbox1拖放到listbox2,但是当我从listbox1中选择多个项目时该怎么办?
I can drag one item from listbox1 and drop it to listbox2 but how can i do it when i select multi items from listbox1?
private void FrmCh3_Load(object sender, EventArgs e)
{
listBox2.AllowDrop = true;
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
listBox1.DoDragDrop(listBox1.SelectedItem, DragDropEffects.Copy);
}
private void listBox2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void listBox2_DragDrop(object sender, DragEventArgs e)
{
listBox2.Items.Add(e.Data.GetData(DataFormats.Text));
}
推荐答案
您只需将整个SelectedItems
属性设置为拖放数据即可复制:
You can simply copy set the entire SelectedItems
property as the drag/drop data:
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
listBox1.DoDragDrop(listBox1.SelectedItems, DragDropEffects.Copy);
}
private void listBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetData(typeof (ListBox.SelectedObjectCollection)) != null)
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void listBox2_DragDrop(object sender, DragEventArgs e)
{
var items = (ListBox.SelectedObjectCollection)e.Data.GetData(typeof (ListBox.SelectedObjectCollection));
foreach (var item in items)
{
listBox2.Items.Add(item);
}
}
这篇关于如何在C#中的2个列表框之间拖动多个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!