ListBox MultiSelect拖放问题 [英] ListBox MultiSelect Drag and Drop problem
问题描述
我正在尝试在Windows窗体的ListBox之间拖放多个项目.我遇到的问题是,如果我选择多个按住Shift键的项目并尝试将其拖放而不释放键,则会出现错误.实际上,即使ListBox中突出显示了多个项目,SelectedIndices和SelectedItems只是显示了一个项目,我首先单击了该项目.
I'm trying to do a drag and drop of multiple items between ListBox in windows forms. The problem I'm having is if I select multiple items holding the Shift key and try to drag and drop it without release the the key I get an error. Actually the SelectedIndices and SelectedItems just show 1 item, the one I clicked first, even though multiple items are highlighted in the ListBox.
我正在使用SelectionMode = MultiExtended
I'm using SelectionMode = MultiExtended
void ZListBox_MouseMove(object sender, MouseEventArgs e)
{
if (isDraggingPoint.HasValue && e.Button == MouseButtons.Left && SelectedIndex >= 0)
{
var pointToClient = PointToClient(MousePosition);
if (isDraggingPoint.Value.Y != pointToClient.Y)
{
lastIndexItemOver = -1;
isDraggingPoint = null;
var dropResult = DoDragDrop(SelectedItems, DragDropEffects.Copy);
}
}
}
似乎,如果在执行"DoDragDrop"之前不释放鼠标左键,则不会选择项目,并且如果我尝试从另一个ListBox中获取SelectedIndices,则Count是选定的项目",但是当我尝试浏览列表时,出现了IndexOutOfRangeException.
It seems that if I don't release the left mouse button before I do "DoDragDrop", the items aren't selected and also if I try to get the SelectedIndices from the other ListBox, the Count is the number of "selected items", but when I try to navigate the list, I get a IndexOutOfRangeException.
有什么解决方法吗?
重现此问题的示例代码:(复制:1-选择一个项目2-按住Shift键并单击另一个项目,然后按住Shift键并单击鼠标,然后拖动该项目(如果'if'内有断点,则SelectedItems上只会显示一个项目)
Sample code to reproduce the issue: (To reproduce: 1- Select an item 2- Hold shift and click in another item, than without release shift and mouse button, drag this item (if you have a breakpoint inside the 'if', you'll see just 1 item on SelectedItems))
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
var someList = new List<ListItemsTest>();
someList.Add(new ListItemsTest() { ID = 1, Name = "Name 1" });
someList.Add(new ListItemsTest() { ID = 2, Name = "Name 2" });
someList.Add(new ListItemsTest() { ID = 3, Name = "Name 3" });
someList.Add(new ListItemsTest() { ID = 4, Name = "Name 4" });
someList.Add(new ListItemsTest() { ID = 5, Name = "Name 5" });
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "ID";
listBox1.DataSource = someList;
listBox1.SelectionMode = SelectionMode.MultiExtended;
listBox1.MouseMove += ListBox1_MouseMove;
listBox1.AllowDrop = true;
}
void ListBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && listBox1.SelectedIndex >= 0)
{
var dropResult = DoDragDrop(listBox1.SelectedItems, DragDropEffects.Copy);
}
}
public class ListItemsTest
{
public int ID { get; set; }
public string Name { get; set; }
}
}
推荐答案
仅告诉您我找到了另一个解决方案.如果我在MouseDown事件中将Capture设置为false,则项目将按预期工作,并且我们不需要手动选择.
Just to let you know I found another solution. If I set Capture = false in the MouseDown event, Items will work as expected and we don't need to do the manual selection.
例如:
void ZListBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Capture = false;
}
}
希望有帮助!
这篇关于ListBox MultiSelect拖放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!