简体   繁体   中英

Mouse Move on a cell of my tablelayoutpanel

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 ?

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM