C# 从列表框中拖放
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16591975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Drag and Drop From listBox
提问by tmoore82
I'm trying to build a simple interface that allows users to drop files into a listBoxto add them to a process, and to drag them out to remove them. Everything is working fine, but I'd like to add one feature to make it just a tad more sophisticated.
我正在尝试构建一个简单的界面,该界面允许用户将文件放入 alistBox以将它们添加到进程中,并将它们拖出以将其删除。一切正常,但我想添加一个功能,让它更复杂一点。
Right now, I have the removal of the item tied to the DragLeaveevent, which means that as soon as the mouse leaves the box, the item is removed. But I'd like for users to be able to change their minds. In other words, if they realize they're dragging the wrong file out, I'd like them to be able to move the mouse back into the listBoxand release the mouse to cancel the action. I'm thinking that means I need to be able to capture the MouseUpevent instead of the DragLeaveevent. But that hasn't been successful so far.
现在,我删除了与DragLeave事件相关的项目,这意味着只要鼠标离开框,项目就会被删除。但我希望用户能够改变主意。换句话说,如果他们意识到他们拖出了错误的文件,我希望他们能够将鼠标移回listBox并释放鼠标以取消操作。我认为这意味着我需要能够捕获MouseUp事件而不是DragLeave事件。但这到目前为止还没有成功。
Below is the code I'm currently using for removing files dragged out. How can I modify to keep the files from being removed form the list until the user lets the mouse button go?
下面是我目前用于删除拖出的文件的代码。如何修改以防止文件从列表中删除,直到用户松开鼠标按钮?
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (listBox1.Items.Count == 0)
{
return;
}
int index = listBox1.IndexFromPoint(e.X, e.Y);
string s = listBox1.Items[index].ToString();
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);
}
private void listBox1_DragLeave(object sender, EventArgs e)
{
ListBox lb = sender as ListBox;
lb.Items.Remove(lb.SelectedItem);
}
Edit 2013/05/16
编辑 2013/05/16
The comments and answers so far have been useful, but I realize my question isn't clear enough. In this case, I'm displaying a dialog separate from the parent form that is basically as big as the listBox. When someone drags a file out of the list, they're dragging it off the form completely. Have I backed myself into a corner by doing this? I recognize I'm making it harder than it has to be, but I'd still like to see how it would work if it's possible.
到目前为止的评论和答案很有用,但我意识到我的问题还不够清楚。在这种情况下,我将显示一个与父表单分开的对话框,该对话框基本上与listBox. 当有人将文件拖出列表时,他们将其完全拖出表单。我这样做是否让自己陷入困境?我承认我让它变得比它更难,但如果可能的话,我仍然想看看它是如何工作的。
采纳答案by B L
Here's a fairly quick hack approach to gaining the functionality you want:
这是获得所需功能的相当快速的黑客方法:
public object lb_item = null;
private void listBox1_DragLeave(object sender, EventArgs e)
{
ListBox lb = sender as ListBox;
lb_item = lb.SelectedItem;
lb.Items.Remove(lb.SelectedItem);
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (lb_item != null)
{
listBox1.Items.Add(lb_item);
lb_item = null;
}
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
lb_item = null;
if (listBox1.Items.Count == 0)
{
return;
}
int index = listBox1.IndexFromPoint(e.X, e.Y);
string s = listBox1.Items[index].ToString();
DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
lb_item = null;
}
Every time the user drags an item out of the box, it's saved temporarily until the user drops it somewhere else or mouses down on a new item in the list.
Note the important part of this is detecting when and where the user let's go of that mouse, which is the rationale behind handling the DragDropevent of Form1, the parent of listBox1.
每次用户将项目拖出框时,它都会暂时保存,直到用户将其放在其他地方或将鼠标移到列表中的新项目上。请注意,其中的重要部分是检测用户何时何地放开该鼠标,这是处理 的DragDrop事件的基本原理Form1, 的父级listBox1。
Depending on the sophistication and density of the rest of your layout, where you handle DragDropcould be much different for you. This is why it's kind of "hacky", but it's also quite simple. It shouldn't matter, though, where or how many times you null lb_itemsince it pertains only to that specific ListBox.
根据布局其余部分的复杂程度和密度,您处理的位置对您来说DragDrop可能大不相同。这就是为什么它有点“hacky”,但它也很简单。但是,您在何处或多少次 null 应该无关紧要,lb_item因为它仅与特定的ListBox.
I suppose another way to do it would be to track the user's mouse states and act accordingly, which may be more appropriate for you if it's inconceivable to handle a lot of DragDropstuff.
我想另一种方法是跟踪用户的鼠标状态并相应地采取行动,如果无法处理很多DragDrop东西,这可能更适合您。
EDIT: If you wanted to be REAL thorough, you could enumerate through everycontrol of the base form using foreachand programmatically append a handler for the DragDrop event to that control, then remove it when done... but that may be getting a little nutty. I'm sure someone has a better approach.
编辑:如果您想要真正彻底,您可以使用基本表单的每个控件进行枚举,foreach并以编程方式将 DragDrop 事件的处理程序附加到该控件,然后在完成后将其删除......但这可能会变得有点疯狂. 我敢肯定有人有更好的方法。

