Swapping controls in TableLayoutPanel
Hi,
Basically I want to be able to drag drop controls inside the table layout panel where the control in the cell where i drop to swap position with the control where i drag from. I already managed to do the drag and drop part, but it doesn't swap position!
I have the table layout panel created using the form designer and the cells populated with checkbox controls (which i also created using the form designer). I want to be able to drag and drop the checkbox controls (swapping the controls by doing so) within the table layout panel cells during runtime, just like what i am able to do using the form designer during design time. I already managed to drag and drop controls within the table layout panel, But my problem now is when i drag and drop a control over another cell, the original checkbox control in the cell doesn't swap position with control i am dragging over.
This is what i did inside the DrapDrop function.
Code:
private void tableLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
CheckBox cb = e.Data.GetData(typeof(CheckBox)) as CheckBox;
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(cb, ColumnIndex, RowIndex);
//cb.Invalidate();
}
Re: Swapping controls in TableLayoutPanel
anyone?
I have the following piece of codes now; but i just don't understand why it doesn't work!!!
Could anyone point out why it doesn't work correctly? The cell position i got is wrong sometimes, but not always. why is this so??
Code:
int _orginRow = -1;
int _orginCol = -1;
private void tableLayoutPanel1_DragEnter(object sender, DragEventArgs e)
{
CheckBox cb = e.Data.GetData(typeof(CheckBox)) as CheckBox;
e.Effect = DragDropEffects.Move;
Point loc = this.tableLayoutPanel1.PointToClient(new Point(e.X, e.Y));
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];
}
_orginCol = ColumnIndex;
_orginRow = RowIndex;
}
private void tableLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
CheckBox cb = e.Data.GetData(typeof(CheckBox)) as CheckBox;
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];
}
Control l_control = this.tableLayoutPanel1.GetControlFromPosition(ColumnIndex, RowIndex);
this.tableLayoutPanel1.Controls.Remove(l_control);
this.tableLayoutPanel1.Controls.Remove(cb);
this.tableLayoutPanel1.Controls.Add(l_control, _orginCol, _orginRow);
this.tableLayoutPanel1.Controls.Add(cb, ColumnIndex, RowIndex);
}
Re: Swapping controls in TableLayoutPanel
What about, rather than catching the drag/drop in the table panel, you catch the drag/drop of the individual controls within the table panel?
I think from a control you can find out what cell it is in, else derive your control and keep it's x,y up to date, eg:
Code:
public class DragableCheckBox : CheckBox
{
Point _point(); // x,y of this CheckBox in the TableLayoutControl
...
// DragDrop handler etc...
}
// somewhere else...
public List<DragableCheckBox> _dragableCheckBoxes;
// possibly keep this sorted by Y aseceending, then X ascending, so that you can more easily
// repopulate the TableLayoutControl when required by simply iterating through this list and adding
// them to the TableLayoutControl
When you drop, you can swap the x and y's of the two DragableCheckBox's and then (in a worst case scenario) repaint the whole of the TableLayoutPanel again by clearing it and adding the DragableCheckBox's again. If it's a small TableLayoutPanel, this may suffice, especially if you enable the DubbleBuffered property on the Main form...
Having said all of that, I do find that dynamically adding/removing items from a TableLayoutControl can be a bit painful as it does seem to assume a lot!!