0

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.

2
  • You're doing an awful lot of processing inside the MouseMove event handler... You realize that is being called 10-20 times per second as the mouse moves over the area of the panel? Commented Dec 26, 2009 at 21:23
  • yes i realize that but havent found another way to do it yet. if i remove the tooltip code it behaves normally and the cpu is not stretched. Commented Dec 26, 2009 at 21:29

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
i see what you mean. will try to store better state information. working on it. will report when i have made this change. thanks.
made progress already! little bugs are there but will solve it and post it later. thanks to everyonefor the help.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.