C#拖放从Control [英] C# Drag and Drop From listBox

查看:162
本文介绍了C#拖放从Control的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个简单的界面,允许用户将文件拖放到一个的ListBox 将它们添加到一个进程,并将其拖出删除它们。一切工作正常,但我想补充一个特点,使其只是一点点更复杂。

I'm trying to build a simple interface that allows users to drop files into a listBox to 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.

现在,我尽快具有去除绑 DragLeave 事件的项目,这意味着鼠标离开盒时,该项目被删除。但我想为用户能够改变他们的想法。换句话说,如果他们意识到他们拖着出去了错误的文件,我希望他们能够将鼠标移动回的ListBox 然后松开鼠标取消该操作。我想这意味着我需要能够捕捉到的MouseUp 事件而不是 DragLeave 事件。但是一直没有成功为止。

Right now, I have the removal of the item tied to the DragLeave event, 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 listBox and release the mouse to cancel the action. I'm thinking that means I need to be able to capture the MouseUp event instead of the DragLeave event. But that hasn't been successful so far.

下面是我目前使用的删除文件的代码拖出来。我要如何从被删除的形式列表保存文件,直到用户可以让鼠标按钮去了?

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);
}

修改2013年5月16日

的意见和答案至今是有益的,但​​我知道我的问题不够清楚。在这种情况下,我显示从父窗体单独一个对话框,基本上是一样大的的ListBox 。当有人拖动文件淘汰之列,他们完全拖动它关闭的形式。我有自己的支持成这样一个角落?我承认我做更难比它是,但我还是想看看是否有可能它是如何工作的。

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.

推荐答案

下面是一个相当快的黑客的方法来获得您想要的功能:

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;
}



每当用户拖动某项开箱即用,它暂时保存,直到用户放置在别的地方,或将鼠标向下列表中的一个新项目。
注本的重要组成部分,正在检测的时间和地点的用户让我们去,小鼠,这是什么道理背后处理<$ C $的的DragDrop 事件C> Form1中, listBox1中。

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 DragDrop event of Form1, the parent of listBox1.

根据您的布局,在那里你处理的DragDrop 的其余部分的复杂性和密度可能是很大的不同您。这就是为什么它是一种哈克,但它也很简单。它不应该的问题,不过,在这里还是多少次,你空 lb_item ,因为它仅适用于特定的ListBox

Depending on the sophistication and density of the rest of your layout, where you handle DragDrop could 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_item since it pertains only to that specific ListBox.

我想另一种方式来做到这将是跟踪用户的鼠标状态和采取相应的行动,这可能更适合你,如果这是不可想象的,以处理大量的的DragDrop 的东西

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 DragDrop stuff.

编辑:如果你想成为真正的深入,你可以通过的每个枚举的使用的foreach 和编程追加dragDrop事件到控制处理程序基本形式的控制权,然后完成时将其删除......但可能会得到一个小坚果。我敢肯定有人有更好的办法。

If you wanted to be REAL thorough, you could enumerate through every control of the base form using foreach and 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.

这篇关于C#拖放从Control的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆