Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How to manipulate cells in a TableLayoutPanel
 

How to manipulate cells in a TableLayoutPanel

Hello,

My name is Serge, hopefully I can get some more information and help from here.

My problem/question is the following.

Is it possible to drag labels in a TableLayoutPanel ? Then when you drag the label in a cell it automaticly 'snaps' in the cell. It should also be possible to drag it back out of the cell.

My main goal is to make some sort of agenda. I need a daylook (with minutes) and there it should be possible to put in an event for that time.

My thoughts were to use TableLayoutPanel --> in the first column I would put the time 08:00-08:30 , 08:30-09:00, and so on...

And in the 2nd column I would make it possible to "drag" en "drop" the events which are related with this time.

Is this possible ? Or should I look further into other controls ?

sorry for my bad English.

Kind regards

*edit*: I use VB

KaizerV3  Friday, May 30, 2008 12:52 PM

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

Zhi-Xin Ye  Monday, June 02, 2008 8:34 AM

Anyone ? Sad
KaizerV3  Monday, June 02, 2008 7:05 AM

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

Zhi-Xin Ye  Monday, June 02, 2008 8:34 AM

Many many thanks Sir.

You have helped me alot ! I converted the code to VB made a few minor changes and it works like a charm !

Greetings from Belgium

KaizerV3  Monday, June 02, 2008 9:11 AM
Hi, I tried using this code in some software I am creating. I swapped the label for buttons and when dragged they move fine, however after a number of these operations the ability to drag it to the bottom stops working.

If you set CellBorderStyle you can see that every drag increases the border width on some of the cells (As if the cells are not being destroyed, but RowCount never increases in size).

I have no idea how to stop this occurring, If you then start moving the buttons back onto the cell border lines that appear the lines disappear.

Anyone understand why this happens?
Sres  Thursday, September 25, 2008 2:34 PM
this helped me a great deal, I am using a contextMenuStrip that appears on a right click event if the table is clicked, now I can add a control from my menu into the cell that was clicked.

thanks again!

-mark
DotNetNewb  Wednesday, October 08, 2008 6:08 PM

You can use google to search for other answers

Custom Search

More Threads

• Resx files not available in designer when in a project subfolder
• Access Keys do not display correctly
• AutoComplete ComboBox with DynamicItemsList
• using windows controls with unmanaged code
• Data grid enter event...
• Dynamic Windows Form UI Creation
• Main form layout and screen resolution help
• Adding Controls to a UserControl on a form
• UserControl custom properties
• I need big help to create a new save as Dialog box control