happy holidays!
i have a tablelayoutpanel (10×10). within each cell i have a picturebox which are disabled (enabled = false).
i am trapping mouse move over the table to catch mouse movement. here is the code:
private void tableLayoutPanelTest_MouseMove(object sender, MouseEventArgs e)
{
if (!placeShip)
{
c = tableLayoutPanelTest.GetControlFromPosition(homeLastPosition.Column, homeLastPosition.Row);
if (c.GetType() == typeof(PictureBox))
{
PictureBox hover = new PictureBox();
hover = (PictureBox)(c);
hover.Image = Properties.Resources.water;
}
Point p = tableLayoutPanelTest.PointToClient(Control.MousePosition);
Control picControl = tableLayoutPanelTest.GetChildAtPoint(p);
if (picControl != null)
{
TableLayoutPanelCellPosition me = tableLayoutPanelTest.GetCellPosition(picControl);
if (picControl.GetType() == typeof(PictureBox))
{
PictureBox thisLocation = new PictureBox();
thisLocation = (PictureBox)(picControl);
thisLocation.Image = Properties.Resources.scan;
homeLastPosition = me;
}
}
}
toolTipApp.SetToolTip(tableLayoutPanelTest, tableLayoutPanelTest.GetCellPosition(c).ToString());
}
when i run this the tooTipApp starts consuming upto 56% of the CPU. so there is something wrong.
also the picturebox image changing code stops working for some reason.
any help is very welcome!
thank you.
A few thoughts:
PictureBoxcalledhover– why? This code doesn’t seem to do anything and it’s almost certainly going to slow the loop down. I think you meant to just declarehoverand cast it fromc, but you’re actually creating a newPictureBoxinstance and just throwing it away.hover, as far as I can tell – so you end up allocating tons of memory and window handles. In general you should avoid creating new objects at all inside aMouseMovehandler (small ones like hit tests are sometimes OK). As with the previous point – you probably didn’t mean to write thenew PictureBox().PointToClient(Control.MousePosition)when theMouseMoveevent already gives you the control-specific mouse position (e.Xande.Y). This is costing you more time than it should.SetToolTipon everyMouseMove. You should only be invoking this when the tooltip has actually changed. You need to set a flag on which cell or control the tooltip was last displayed for, check for changes, then callSetToolTip.