none
How to dispose dynamically created button

    Question

  • Hi Guys, in my application, i got many menu item where the users can click on the items to display files in the datagrids.
    In the main button, i'll verify which file it's and then refer to a specific funtion. In this exmpale, i'm passing to the `mainFunction();`
    and i'm creating a button dynamically and assigning another function to the dynamically created button.

    Until here, everything is ok. When i try to open another file from menu, i want to dispose the dynamically created button 
    becasue i'll create buttons dynamically for each file have to perform a different calculation.

    How can i dispose/remove dynamically created button?? 

    private void b_calculate_Click(object sender, EventArgs e) // First Button
    {
    	mainFunction();
    }
    private void mainFunction() 
    {
    	MessageBox.Show("You are using : A1-B1");
    
    	Button Calculate = new Button();  //Creating button dynamically
    	Calculate.Text = "Calculate-Proof";
    	Calculate.Size = new Size(106, 51);
       
    	Calculate.Font = new Font(this.Font, FontStyle.Bold);
    	
    	Calculate.Location = new Point(832, 292);
    	Calculate.Click += delegate
    	{
    	   
    		testfunction();
    
    	};
    	 g_RTRadGroupBox.Controls.Add(Calculate);
    }
    
    private void testfunction()
    {
    	MessageBox.Show("Please verify the data in the Interface/File");
    	
    }

    Just to show you guys, this is how i open the file through menu click:

    private void m_test111_Click(object sender, EventArgs e)
    {
    	OpenFileXML(@"test111.xml");
    }
    

    Sunday, November 18, 2012 9:47 AM

Answers

  • There properties like ID or Name to set the identity for the control. Then you add it to group box controls collection. When you don't need the button anymore you can look the button from controls collection of the group box, retrieve it, remove it from the group box controls and dispose.

    Of couse if you know you don't neet the button after it is clicked one time, then you can remove and dispose the button in event handler after the click is handled like in following example.

            private void button1_Click(object sender, EventArgs e)
            {
                Button dynButton = new Button();
                dynButton.Text = "Calculate";
                dynButton.Click += dynButton_Click;
    
                groupBox1.Controls.Add(dynButton);
            }
    
            void dynButton_Click(object sender, EventArgs e)
            {
                // for example to remove
                Button button = (Button)sender;
    
                // code that is executed on click
    
                // remove and dispose
                groupBox1.Controls.Remove(button);
                button.Dispose();
            }

    • Marked as answer by FF_DAPI Sunday, November 18, 2012 11:43 AM
    Sunday, November 18, 2012 11:41 AM

All replies

  • You might not need to think that because dynamically created button will be disposed when the group box is disposed where you add your button. If button might need to be disposed before that then you need to get access to it from the controls collectionof the group box, assign unique identifier to help you to find correct one, and call dispose when you don't need button anymore.
    Sunday, November 18, 2012 10:07 AM
  • You might not need to think that because dynamically created button will be disposed when the group box is disposed where you add your button. If button might need to be disposed before that then you need to get access to it from the controls collectionof the group box, assign unique identifier to help you to find correct one, and call dispose when you don't need button anymore.

    Yes, you are correct, i forgot about that. The groupbox is always constant. Could you please explain to me, how can i assign unique identifier to help me to find correct one, and call dispose when i don't need button anymore ???

    Thank you very much.

    Sunday, November 18, 2012 10:11 AM
  • There properties like ID or Name to set the identity for the control. Then you add it to group box controls collection. When you don't need the button anymore you can look the button from controls collection of the group box, retrieve it, remove it from the group box controls and dispose.

    Of couse if you know you don't neet the button after it is clicked one time, then you can remove and dispose the button in event handler after the click is handled like in following example.

            private void button1_Click(object sender, EventArgs e)
            {
                Button dynButton = new Button();
                dynButton.Text = "Calculate";
                dynButton.Click += dynButton_Click;
    
                groupBox1.Controls.Add(dynButton);
            }
    
            void dynButton_Click(object sender, EventArgs e)
            {
                // for example to remove
                Button button = (Button)sender;
    
                // code that is executed on click
    
                // remove and dispose
                groupBox1.Controls.Remove(button);
                button.Dispose();
            }

    • Marked as answer by FF_DAPI Sunday, November 18, 2012 11:43 AM
    Sunday, November 18, 2012 11:41 AM