我有两个面板,一个在负载上填充控件。当用户将控件拖动到另一个面板时,它将控件从原始面板移动到新面板,我宁愿像控件一样复制并放在另一个面板中。我需要能够多次将同一个控件拖到面板中。我怎样才能实现这个目标?我已经尝试将拖动效果更改为复制,但这似乎没有诀窍。
void panel_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
void panel_DragDrop(object sender, DragEventArgs e)
{
Button data = (Button)e.Data.GetData(typeof(Button));
FlowLayoutPanel _destination = (FlowLayoutPanel)sender;
FlowLayoutPanel _source = (FlowLayoutPanel)data.Parent;
if (_source != _destination)
{
// Add control to panel
data.Size = new Size(_destination.Width, 85);
_destination.Controls.Add(data);
// Reorder
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(data, index);
// Invalidate to paint!
_destination.Invalidate();
_source.Invalidate();
}
else
{
// Just add the control to the new panel.
// No need to remove from the other panel, this changes the Control.Parent property.
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(data, index);
_destination.Invalidate();
}
}
答案 0 :(得分:1)
如果要复制控件,则需要实际复制正在复制的控件。执行此操作的最佳位置可能是您将模式从移动更改为复制的位置。您可以将副本保留在原始位置,然后继续移动原件或移动副本。
所以你在哪里:
Button data = (Button)e.Data.GetData(typeof(Button));
您需要克隆按钮或创建新的Button
并手动设置属性。克隆将是更好的解决方案。