2013-09-07 6 views
2

나는 셀이 16 개인 tableLayoutPanel입니다. 15 개의 세포가 통제를받습니다. 런타임시 한 셀에서 다른 셀로 컨트롤을 이동할 수 있기를 원합니다.한 셀에서 다른 셀로 항목 이동

나는

private void button15_Click(object sender, EventArgs e) 
    { 
     tableLayoutPanel1.Controls.Remove(button15); 

     tableLayoutPanel1.Controls.Add(button15, 3, 3); 
    } 

이 잘 작동을 사용했다하지만 난이 일을 더 나은 방법이 있는지 알고 싶어 ???

+1

SetRow(), SetColumn() 및 SetCellPosition()이 마지막 2 개를 마지막으로 만드는 경우 - 3 개의 메서드는 첫 번째 인수로 문제의 컨트롤을 가져온 다음 setrow 2 º 인수는 원하는 행에 setcolumn은 같지만 원하는 열에 대해), SetCellPosition 2º 인수는 행과 열을 모두 설정하는 TableLayoutPanelCellPosition 유형을 취합니다. 이처럼 필요한 컨트롤을 얻습니다. Control c = tableLayoutPanel1.GetControlFromPosition (0, 0); 열 0 - 행 0. 도움이 되길 바랍니다. – terrybozzio

+0

@terrybozzio 감사합니다. 내가 확인해 보겠습니다. –

답변

3

에는 부모 컨트롤이있는 경우에만 컨트롤을 이동할 수 있습니다 (실제로 일부 컨트롤에는 예외가 있지만 실제로는 부모가 없습니다). 따라서 여기에서 아이디어는 TableLayoutPanel의 컨트롤을 이동하려는 경우 부모를 으로 설정해야합니다. 마우스를 누르고 있으면 움직일 때 컨트롤의 위치가 마우스가 된 후 새 부모에 놓입니다. 우리가 다시 TableLayoutPanel 컨트롤의 부모를 설정해야합니다, 물론 우리는 드롭 다운 셀 위치를 찾아 SetCellPosition 메서드를 사용하여 TableLayoutPanel에 컨트롤을 배치해야합니다, 여기 당신을위한 데모 코드입니다 (작품

public partial class Form1 : Form { 
    public Form1() { 
     InitializeComponent(); 
     //This will prevent flicker 
     typeof(TableLayoutPanel).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(tableLayoutPanel1, true, null); 
    } 
    Point downPoint; 
    bool moved; 
    //This is used to store the CellBounds together with the Cell position 
    //so that we can find the Cell position later (after releasing mouse). 
    Dictionary<TableLayoutPanelCellPosition, Rectangle> dict = new Dictionary<TableLayoutPanelCellPosition, Rectangle>(); 
    //MouseDown event handler for all your controls (on the tableLayoutPanel1) 
    private void Buttons_MouseDown(object sender, MouseEventArgs e) { 
     Control button = sender as Control; 
     button.Parent = this;    
     button.BringToFront();    
     downPoint = e.Location;    
    } 
    //MouseMove event handler for all your controls (on the tableLayoutPanel1) 
    private void Buttons_MouseMove(object sender, MouseEventArgs e) { 
     Control button = sender as Control; 
     if (e.Button == MouseButtons.Left) { 
      button.Left += e.X - downPoint.X; 
      button.Top += e.Y - downPoint.Y; 
      moved = true; 
      tableLayoutPanel1.Invalidate(); 
     } 
    } 
    //MouseUp event handler for all your controls (on the tableLayoutPanel1) 
    private void Buttons_MouseUp(object sender, MouseEventArgs e) { 
     Control button = sender as Control; 
     if (moved) { 
      SetControl(button, e.Location); 
      button.Parent = tableLayoutPanel1; 
      moved = false; 
     } 
    } 
    //This is used to set the control on the tableLayoutPanel after releasing mouse 
    private void SetControl(Control c, Point position) { 
     Point localPoint = tableLayoutPanel1.PointToClient(c.PointToScreen(position)); 
     var keyValue = dict.FirstOrDefault(e => e.Value.Contains(localPoint)); 
     if (!keyValue.Equals(default(KeyValuePair<TableLayoutPanelCellPosition, Rectangle>))) { 
      tableLayoutPanel1.SetCellPosition(c, keyValue.Key); 
     } 
    } 
    //CellPaint event handler for your tableLayoutPanel1 
    private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) { 
     dict[new TableLayoutPanelCellPosition(e.Column, e.Row)] = e.CellBounds; 
     if (moved) { 
      if (e.CellBounds.Contains(tableLayoutPanel1.PointToClient(MousePosition))) { 
       e.Graphics.FillRectangle(Brushes.Yellow, e.CellBounds); 
      } 
     } 
    } 
} 

enter image description here

:) 좋은, 내가이 데모에서 2 Button S를 사용하여, 당신은 당신이 원하는 모든 컨트롤로 교체 할 수 있습니다3210
+0

이 코드를 사용하면 Button을 TableLayoutPanel의 다른 셀로 이동할 수 있지만 버튼을 다시 클릭하면 다른 위치로 자동 전환됩니다. 이 문제를 해결하도록 도와 주시겠습니까? – hung34k

관련 문제