Click here to Skip to main content
15,907,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 buttons. Every time i press on either of them, the form is flickering and is frozen. More times i click on the buttons, the LONGER the frozen time it takes. I searched on internet and find that Controls.Clear(); is cleaning from array of controls but not from memory. I tried to clear the memory, but i realize i have no idea how to do it. I tried it and got 2 errors for 2 versions of loop.
        private void button01_Click(object sender, EventArgs e)
        {
            Controls.Clear();            InitializeButtons();
            // ClearALLControls();
        } 

        private void button02_Click(object sender, EventArgs e)
        {
            Controls.Clear();            InitializeButtons();
        }

        private void button03_Click(object sender, EventArgs e)
        {
            Controls.Clear();            InitializeButtons();
        }


        void ClearALLControls() //this method not working - got 2 errors
        {
            foreach (Control item in this.Controls)
            {
                item = null; //error@ [item]
//Cannot assign to 'item' because it is a 'foreach iteration variable'	

            }
//so... it's a foreach problem? i try it with for loop then

            for (int i = 0; i < Controls.Count; i++)
            {
                Controls[i] = null; //error@ [Controls[i]]
//System.Windows.Forms.Control.ControlCollection.this[int]' cannot be assigned to -- it is read only	
            }
        }


What I have tried:

......................................
Posted
Updated 20-Oct-18 10:17am
v2

1 solution

At a guess, it's your InitializeButtons that is doing the damage.
I suspect that you are adding click handlers to the existing buttons and putting them back in the Controls collection.
And handlers are chained delegates: adding the same handler twice means it's called twice, then three times four times, five times, ... so the amount of time the handler takes to execute increases each time it is pressed.

What you show there doesn't show that bit - that's why it's a guess - but the code you do show is ... um ... rather odd. It looks like you don't know what you are doing, and are coding by "hit and hope" rather than thinking it through carefully, because I can't work out what you think you are trying to do that that code might be good solution to!
 
Share this answer
 
Comments
_Q12_ 20-Oct-18 16:44pm    
You are right, there are declaration of events inside my method.
I though of them to be the cause for a glimpse of a second, but i choose to search on internet.
_Q12_ 20-Oct-18 16:53pm    
Excellent detective eye you have. Thank you ! It resolved my problem.
Now is working purrfectly.
5 stars
OriginalGriff 20-Oct-18 17:36pm    
You're welcome!

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