Hi,
You can make the TableLayoutPanel allow drop by setting the AllowDrop propertyto true, then handle itsDragDrop, DragEnter events to move the label to destination, see my sample for the details
Code Snippet
void Form1_Load(object sender, EventArgs e)
{
this.tableLayoutPanel1.AllowDrop = true;
}
void label1_MouseDown(object sender, MouseEventArgs e)
{
this.label1.DoDragDrop(this.label1, DragDropEffects.Move);
}
void tableLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
Label lb = e.Data.GetData(typeof(Label)) as Label;
Point loc = this.tableLayoutPanel1.PointToClient(new Point(e.X, e.Y));
//detemine the cell location
int ColumnIndex = -1;
int RowIndex = -1;
int x = 0;
int y = 0;
while (ColumnIndex <= this.tableLayoutPanel1.ColumnCount)
{
if (loc.X < x)
{
break;
}
ColumnIndex++;
x += this.tableLayoutPanel1.GetColumnWidths()[ColumnIndex];
}
while (RowIndex <= this.tableLayoutPanel1.RowCount)
{
if (loc.Y < y)
{
break;
}
RowIndex++;
y += this.tableLayoutPanel1.GetRowHeights()[RowIndex];
}
this.tableLayoutPanel1.Controls.Add(lb, ColumnIndex, RowIndex);
}
void tableLayoutPanel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
Best Regards
Zhi-xin
|