views:

203

answers:

2

happy holidays!

i have a tablelayoutpanel (10x10). 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.

+1  A: 

You will get a lot of performance back if you avoid setting the tooltip text when it hasn't changed.

Other than that I want to echo the comment. This is a lot of processing for a mouse handler.

You should be trying to do an early test to see if the mouse is still over the same thing it was on the last move, and skipping most of the code in that case.

John Knoeller
+1  A: 

A few thoughts:

  • You're creating another PictureBox called hover - 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 declare hover and cast it from c, but you're actually creating a new PictureBox instance and just throwing it away.
  • You're also never disposing of 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 a MouseMove handler (small ones like hit tests are sometimes OK). As with the previous point - you probably didn't mean to write the new PictureBox().
  • You use PointToClient(Control.MousePosition) when the MouseMove event already gives you the control-specific mouse position (e.X and e.Y). This is costing you more time than it should.
  • Probably the most important, you're invoking SetToolTip on every MouseMove. 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 call SetToolTip.
Aaronaught
i added this check: if (c.Location != e.Location) { do stuff}but did not get any performance gains. the line:toolTipApp.SetToolTip(tableLayoutPanelTest, tableLayoutPanelTest.GetCellPosition(c).ToString());is eating up cpu still. without this line cpu load is 10 - 12% but with this code it goes to 54%. not sure why.
iEisenhower
First of all, you don't specify where you added that line; second, that's not even close to being a reliable check. The cursor position is almost never going to be the exact top left pixel of the picture box. As stated previously, you need to store state information about either the table row/cell or the control itself that was last hovered over, and compare against this.
Aaronaught
i see what you mean. will try to store better state information. working on it.will report when i have made this change. thanks.
iEisenhower
made progress already! little bugs are there but will solve it and post it later. thanks to everyonefor the help.
iEisenhower