I have problem when i try to set base.OnMouseDown(e) on MouseDown event or another one it’s crashing for the reason System.StackOverflowException: 'An exception of type 'System.StackOverflowException' was thrown.' and in exception details i get [The above 2 frame(s) have been repeated 6289 times], I want to get a part of this (only for click on painted button) Foreach loop to create 100 buttons, painting all buttons at same time as to prevent flicker but i don’t know how to fix this.
private void Dashboard_MouseDown(object sender, MouseEventArgs e)
{
for (int y = 0; y < alts; ++y)
{
for (int x = 0; x < 1; ++x)
{
if (new Rectangle(735, alts_Y + 5, 45, 15).Contains(e.Location))
{
this.Invalidate();
}
alts_Y += 72;
}
}
base.OnMouseDown(e);
}
>Solution :
I guess you’re messing up an event handler (your method Dashboard_MouseDown()) with triggering the event here (by calling OnMouseDown()).
OnMouseDown() triggers your event handler Dashboard_MouseDown(). Calling OnMouseDown() executes Dashboard_MouseDown(). That leads to an infinite loop and result in a StackOverflowException.
In the example you linked, the protected OnMouseDown() method is overridden and needs to call the base.OnMouseDown() to trigger the registered events.
If you override the base class you need to override the protected OnMouseDown(). If you want to register to the event use your Dashboard_MouseDown() but without the call of base.OnMouseDown().