에는 부모 컨트롤이있는 경우에만 컨트롤을 이동할 수 있습니다 (실제로 일부 컨트롤에는 예외가 있지만 실제로는 부모가 없습니다). 따라서 여기에서 아이디어는 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);
}
}
}
}
:) 좋은, 내가이 데모에서 2
Button
S를 사용하여, 당신은 당신이 원하는 모든 컨트롤로 교체 할 수 있습니다3210
SetRow(), SetColumn() 및 SetCellPosition()이 마지막 2 개를 마지막으로 만드는 경우 - 3 개의 메서드는 첫 번째 인수로 문제의 컨트롤을 가져온 다음 setrow 2 º 인수는 원하는 행에 setcolumn은 같지만 원하는 열에 대해), SetCellPosition 2º 인수는 행과 열을 모두 설정하는 TableLayoutPanelCellPosition 유형을 취합니다. 이처럼 필요한 컨트롤을 얻습니다. Control c = tableLayoutPanel1.GetControlFromPosition (0, 0); 열 0 - 행 0. 도움이 되길 바랍니다. – terrybozzio
@terrybozzio 감사합니다. 내가 확인해 보겠습니다. –