Winform C中的错误拖动# [英] Error Dragdrop in Winform C#
问题描述
为什么我拖动Panel时不会在Panel中自动调整大小并显示在新面板中。
Why when I drag Panel shall not be automatically resized in Panel old and appears in the new Panel.
private void PanelMove(object sender, MouseEventArgs e)
{ Panel p = new Panel();
p = (Panel)sender;
p.DoDragDrop(p, DragDropEffects.Link);
}
private void PanelDragEnter(object sender, DragEventArgs e)
{
currentpanel = (Panel)e.Data.GetData(e.Data.GetFormats()[0]);
currentpanelParent = (Panel)currentpanel.Parent;
e.Effect = DragDropEffects.Link;
}
private void PanelDrag(object sender, DragEventArgs e)
{
var newpanel = new Panel();
var newpanel2 = new Panel();
newpanel = (Panel)e.Data.GetData(e.Data.GetFormats()[0]);
var ParentPanel =(Panel) ((Panel)sender).Parent;
var totalcontrol = ParentPanel.Controls.Count;
newpanel.Height = 30;
newpanel2 = (Panel)sender;
if (ParentPanel != currentpanelParent)
{
ParentPanel.Controls.Add(newpanel);
ParentPanel.AutoSize = true;
currentpanelParent.Controls.Remove(currentpanel);
currentpanel.AutoSize = true;
}
}
https://dm2302files.storage.live.com/y2p6qaa6OiOq-PxjDs3Kq6URy2q-mgeDSa_2HakfGsRwbRUL6hVwo7qOPJHRWlsj_K0DQtgOkQzs6j5tvLzPzi0ry-0KMm_OqZGndSYwbks2DIFi3fViSiExqtKN0c0EKlht1Sa1z7i4lCUUcGgF1274Q/Error.jpg?psid = 1
https://dm2302files.storage.live.com/y2p6qaa6OiOq-PxjDs3Kq6URy2q-mgeDSa_2HakfGsRwbRUL6hVwo7qOPJHRWlsj_K0DQtgOkQzs6j5tvLzPzi0ry-0KMm_OqZGndSYwbks2DIFi3fViSiExqtKN0c0EKlht1Sa1z7i4lCUUcGgF1274Q/Error.jpg?psid=1
推荐答案
我不确定你想要实现的目标但是...
你分配新对象而不使用它们:
I'm not sure what you're trying to achieve but...
You allocate new objects without using them:
Panel p = new Panel()
var newpanel = new Panel();
var newpanel2 = new Panel()
拖放应该由包含对象初始化。
在这种情况下(我认为)它是窗口?
Drag and drop should be initialized by the containing object.
In this case (I think) it is the window?
this p.DoDragDrop(p, DragDropEffects.Link);
你应该在drop事件处理程序中查找特定数据,否则这是一种不安全的做法。
You should look for a specific data in the drop event handler, otherwise it is an unsafe practice.
if(e.Data.GetDataPresent(typeof(Panel)))
newpanel = (Panel)e.Data.GetData(e.Data.GetFormats()[0] typeof(Panel));
首先尝试从上一个容器中删除面板然后将其添加到新的。
Try to firstly remove the panel from it's previous container and then add it to the new one.
这篇关于Winform C中的错误拖动#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!