我需要在面板之间拖动标签。但是当我试图在初始面板中删除标签时,它就会消失。这是我使用的方法的代码:
private void label1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
DoDragDrop(sender, DragDropEffects.All);
}
private void panel_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void panel_DragDrop(object sender, DragEventArgs e)
{
Label src = e.Data.GetData(typeof(Label)) as Label;
src.Location = PointToClient(new Point(e.X, e.Y));
}
为面板启用了AllowDrop。为什么标签会消失,我该如何解决呢?
答案 0 :(得分:1)
标签仍然包含在表单中,因此它只是后面面板。
要么...
(1)将标签带到前面:
private void panel1_DragDrop(object sender, DragEventArgs e)
{
Label src = e.Data.GetData(typeof(Label)) as Label;
src.Location = this.PointToClient(new Point(e.X, e.Y));
src.BringToFront();
}
,或者
(2)使Panel包含Label,并调整Panel的客户端坐标系统的坐标:
private void panel1_DragDrop(object sender, DragEventArgs e)
{
Panel pnl = sender as Panel;
Label src = e.Data.GetData(typeof(Label)) as Label;
src.Location = pnl.PointToClient(new Point(e.X, e.Y));
pnl.Controls.Add(src);
}