Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reproduce a friendly loop for MouseDown event c#

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading