【问题标题】:How can I make images drag and drop-able to reorder them?如何使图像可以拖放以重新排序?
【发布时间】:2016-06-29 11:00:03
【问题描述】:

我正在构建一个应用程序,我希望用户能够在其中对两列表单中的图片进行重新排序。我有一个设置宽度的 flowLayoutPanel,图片通过 OpenFileDialog 添加并缩放到流布局面板宽度的一半(减去滚动条的余量)。

这是我卡住的地方 - 我尝试将图像添加为标签、按钮,现在又添加为图片框,但我不知道如何实际移动它们。我放弃了标签,因为CanSelect 是错误的——尽管我不知道这是否会有所不同——我从按钮继续前进,因为我意识到图片框的存在。我愿意切换我使用的控件,但图像总是需要在两列中。

这是我目前拥有的 DragEnter 和 DragDrop 事件的代码:

private void flowLayoutPanel_6_Cards_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.All;
}
private void flowLayoutPanel_6_Cards_DragDrop(object sender, DragEventArgs e)
{
    MessageBox.Show("dropped");
}

我该如何实现呢?我应该使用哪些控件以及应该查看哪些属性才能实现这一点?

【问题讨论】:

  • 您提到的任何控件都应该可以工作。您是否还编写了鼠标事件之一来启动 D&D?并设置 AllowDrop ?
  • @TaW 我设置了 AllowDrop = true 但我应该如何处理鼠标事件?
  • 在 mouseDown 或 MouseMove 中你应该通过调用DoDragDrop来启动 D&D
  • @TaW 太好了,我没有意识到我也必须这样做!谢谢:)

标签: c# .net winforms user-interface


【解决方案1】:

感谢@TaW 的评论,我现在知道您必须在拖动的任何内容上向 MouseDown 事件添加 DoDragDrop 调用。我现在工作的代码如下(主要感谢this 教程):

private void flowLayoutPanel_6_Cards_DragDrop(object sender, DragEventArgs e)
{
    PictureBox picture = (PictureBox)e.Data.GetData(typeof(PictureBox));    
    FlowLayoutPanel _source = (FlowLayoutPanel)picture.Parent;
    FlowLayoutPanel _destination = (FlowLayoutPanel)sender;

    if (_source != _destination)
    {
        //where did you even get this from?
    }
    else
    {
        Point p = _destination.PointToClient(new Point(e.X, e.Y));
        var item = _destination.GetChildAtPoint(p);
        int index = _destination.Controls.GetChildIndex(item, false);
        _destination.Controls.SetChildIndex(picture, index);
        _destination.Invalidate();

    }
}

private void flowLayoutPanel_6_Cards_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.All;
}

void p_MouseDown(object sender, MouseEventArgs e)
{
    PictureBox p = (PictureBox)sender;
    p.DoDragDrop(p, DragDropEffects.All);
}

【讨论】:

    猜你喜欢
    • 2012-05-08
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多