问题
I have a problem with my TLP. I would like the cell's color to be changed when the mouse is moving over the cells. I trieed different things but nothing works. Do you have an idea how I could get over this problem ?
回答1:
TLPs are not very nice to work with.
You can use the TableLayoutCellPaintEventArgs
to learn about a cell while is being painted and convert the cursor's screen position to the relative one with PointToClient
..
Here is an example, but I'm not sure how well it will work for larger TLPs:
private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
{
tableLayoutPanel1.Invalidate();
}
private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
Point pt = tableLayoutPanel1.PointToClient(Cursor.Position);
using (SolidBrush brush = new SolidBrush(e.CellBounds.Contains(pt) ?
Color.Red : tableLayoutPanel1.BackColor))
e.Graphics.FillRectangle(brush, e.CellBounds);
}
This paints the cell the cursor is over and resets when it leaves. If you want to keep the changed color you will need to store it in an 2d-array and use that as the alternative color. The details will depend on just what you want to achieve.
You may also want to study this post to learn more about working with TLPs..
来源:https://stackoverflow.com/questions/35404110/mouse-move-on-a-cell-of-my-tablelayoutpanel