0

So what i have is a panel that's programmatically filled with custom controls using DockStyle.Top.

What i need is for the panel to get focus somehow when mouse cursor enters the panel so that the user can use mousewheel to scroll the panel.

I don't really want to give each control a handler because there could be hundreds of controls.

One way could be checking for mouse position and check if the panel contains it, which would probably require an extra thread or mousehook but perhaps there's a better way?

Dennis
  • 37,026
  • 10
  • 82
  • 150
Jixi
  • 117
  • 1
  • 13
  • There's a property `Tag`. You can bind one event handler to all of your panels and identify them with `Tag`. This in a case you need something advanced (I don't have any real-world example). Anyway, first argument of handler, contains sender `object sender` you can cast one `Panel myPanel = sender as Panel;`. – Leri Jan 29 '13 at 07:47
  • Create a single handler function. Inside the `InitializeComponent` function, assign the same handler to each control. – sgarizvi Jan 29 '13 at 07:48
  • @PLB That wont work since the panel wont see the mouse since it's under the other controls. – Jixi Jan 29 '13 at 07:55
  • @sgar91 That would still require having a "massive" loop everytime the panel is updated – Jixi Jan 29 '13 at 07:57
  • check http://stackoverflow.com/questions/3005854/net-c-sharp-mouseenter-listener-on-a-control-with-scrollbar – spajce Jan 29 '13 at 08:02

1 Answers1

2

You may implement the MouseDetector class posted by Amen Ayach as an answer to a similar question and activate the form when the mouse hovers it:

void m_MouseMove(object sender, Point p)
{
    Point pt = this.PointToClient(p);
    if (this.ClientSize.Width >= pt.X &&
                    this.ClientSize.Height >= pt.Y &&
                    pt.X > 0 && pt.Y > 0)
    {
        this.Activate();
    }
}

You should also set the Panel's AutoScroll value to true.

panel.AutoScroll = true;
Community
  • 1
  • 1
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
  • 1
    This seems to work perfectly fine with the exception of adding a timer, which is not too bad considering i can use a single instance for any focus i need, thanks! Don't know how i missed that.. even though i read the answer earlier :P – Jixi Jan 29 '13 at 08:13