Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a checkBox inside a usercontrol. My usercontrol is in Form1. When i click on checkBox , it must make Form1 always on top. But (inside my usercontrol.cs) i get this error:

StackOverflowException was unhandled
An unhandled exception of type 'System.StackOverflowException' occurred in LinksPlayer.exe

at the start of this event:
private void checkBoxOnTop_CheckedChanged(object sender, EventArgs e)

What I have tried:

C#
//inside usercontrol
        public event EventHandler checkBoxOnTophandleA;
        private void checkBoxOnTop_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxOnTophandleA != null) checkBoxOnTop_CheckedChanged(null, null);
        }

//and in Form1
        private void Form1_Load(object sender, EventArgs e)
        {
            topMenu1.checkBoxOnTophandleA += new EventHandler(topMenu1_checkBoxOnTophandle);
        }
        void topMenu1_checkBoxOnTophandle(object sender, EventArgs e)
        {
            this.TopMost = false;
        }
Posted
Updated 1-Sep-19 6:14am

1 solution

checkBoxOnTophandleA is null and never becomes anything else. And when checkBoxOnTophandleA is null, then checkBoxOnTop_CheckedChanged calls itself, but because checkBoxOnTophandleA does not become anything other than null, checkBoxOnTop_CheckedChanged keeps calling itself which causes a StackOverflowException because such an Exception happens when you have too many nested method calls[^].
 
Share this answer
 
Comments
_Q12_ 1-Sep-19 12:20pm    
Very nice, but how to un-null it?
Put an example please. Thank you.
Thomas Daniels 1-Sep-19 12:21pm    
I can't really give a suggestion because I'm not sure what else checkBoxOnTophandleA is used for or what would make sense in your code.
_Q12_ 1-Sep-19 12:30pm    
This checkbox was on Form1 and run this code when checked changed event was fired:
if (checkBoxOnTop.Checked) this.TopMost = true;
else this.TopMost = false;
Now i put it in a custom usercontrol but i get this error from it.
Make this control work from a usercontrol and execute that code for Form1 to be ontop or not.
Thanks.
Thomas Daniels 1-Sep-19 12:34pm    
Okay, what I _think_ you want to be doing, is calling checkBoxOnTophandleA instead of checkBoxOnTop_CheckedChanged... so like this:
if (checkBoxOnTophandleA != null) checkBoxOnTophandleA(null, null);
Not tested but I think that's what you're trying to do.
_Q12_ 1-Sep-19 12:44pm    
sweet, it works now. That was it.
Thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900