Mouse Move on a cell of my tablelayoutpanel

馋奶兔 提交于 2020-01-14 05:37:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!