2016-10-17 3 views
2

두 개의 패널과 하나의로드에 컨트롤이 채워집니다. 사용자가 컨트롤을 다른 패널로 드래그하면 컨트롤이 원래 패널에서 새 패널로 이동하기 때문에 다른 패널에 컨트롤 및 배치를 복사하는 것이 좋습니다. 동일한 컨트롤을 여러 번 패널로 드래그 할 수 있어야합니다. 어떻게이 작업을 수행 할 수 있습니까? 드래그 효과를 복사하여 변경하려고 시도했지만 트릭을 수행하지 않는 것 같습니다.복사하는 대신 끌어서 놓기 움직이는 컨트롤

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

Winforms에서 컨트롤 복사본을 만들 수있는 메커니즘이 없습니다. Control 클래스는 너무 많은 속성을 가지며 중첩 될 때 훨씬 나 빠지게됩니다. 대신 컨트롤을 다시 만들어야합니다. 드래그하려는 원래 패널을 만드는 메서드가 있어야합니다. 그리고 드래그가 완료되면 해당 메소드를 다시 실행하십시오. Parent와 Location이라는 두 개의 인수가 필요합니다. –

+0

시계 : http://stackoverflow.com/questions/11407068/how-to-drag-and-drop-a-button-from-one-panel-to-another-panel 감사합니다. jens – schwebbe

답변

1

컨트롤을 복사하려면 실제로 복사 할 컨트롤의 복사본을 가져와야합니다. 이렇게하는 가장 좋은 장소는 모드를 복사에서 복사로 변경하는 곳일 것입니다. 사본을 원래 위치에두고 원본을 계속 이동하거나 사본을 이동할 수 있습니다.

그래서 당신은 여기서

Button data = (Button)e.Data.GetData(typeof(Button)); 

버튼을 복제하거나 새 Button을 작성하고 수동으로 속성을 설정하거나해야합니다. 복제가 더 나은 솔루션이 될 것입니다.