拖放不起作用

时间:2012-01-12 06:53:09

标签: winforms c#-3.0

在Windows窗体中

我有Listview和FlowLayoutPanel

我想从Listview拖到FlowLayoutPanel所以

列表视图中的

我使用DragEnter事件

private void listViewGUI_DragEnter(object sender, DragEventArgs e)
        {
}

并在FlowLayoutPanel中激活DragDrop

private void fpnlDisplayedGUI_DragDrop(object sender, DragEventArgs e)
        {
}

问题是它不起作用他们不输入任何此事件,任何想法如何使他们输入我错过了任何属性

祝你好运

2 个答案:

答案 0 :(得分:2)

以下是显示所需内容的基础知识的简单示例:

public Form1()
{
    InitializeComponent();

    panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
    panel2.AllowDrop = true;
    panel2.DragEnter += new DragEventHandler(panel2_DragEnter);
    panel2.DragDrop += new DragEventHandler(panel2_DragDrop);
}

void panel2_DragDrop(object sender, DragEventArgs e)
{
    //handle the drop here.
}

void panel2_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

void panel1_MouseDown(object sender, MouseEventArgs e)
{
    panel1.DoDragDrop("whatever you want draged.", DragDropEffects.Move);
}

答案 1 :(得分:2)

您将DragEnter事件放在错误的控件上,您必须使用面板。我想我知道你是怎么遇到这个麻烦的,ListView没有任何表明用户开始拖动项目的事件。你需要自己合成它。基本方法是记录鼠标按下位置并使用MouseMove事件检查用户是否已将鼠标移动到足够远以开始拖动。像这样:

    private Point dragMousePos;

    private void listView1_MouseDown(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) dragMousePos = e.Location;
    }

    private void listView1_MouseMove(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) {
            int dx = Math.Abs(e.X - dragMousePos.X);
            int dy = Math.Abs(e.Y - dragMousePos.Y);
            if (dx >= SystemInformation.DoubleClickSize.Width ||
                dy >= SystemInformation.DoubleClickSize.Height) {
                var item = listView1.GetItemAt(dragMousePos.X, dragMousePos.Y);
                if (item != null) listView1.DoDragDrop(item, DragDropEffects.Move);
            }
        }
    }