Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Look at this example we create button myNewButton at runtime by Clicking MyExistingButton .
How we can delete myNewButton by pressing another button after we created?

private void MyExistingButton_Click(object sender, EventArgs e)
    {
    Button myNewButton = new Button();
    myNewButton.Text = "ClickMe";
    myNewButton.Location = new Point((Width - myNewButton.Width) / 2, (Height - myNewButton.Height) / 2);
    myNewButton.Click += new EventHandler(myNewButton_Click);
    Controls.Add(myNewButton);
    }
void myNewButton_Click(object sender, EventArgs e)
    {
    MessageBox.Show("Hello World!");
    }
Posted
Updated 1-Dec-18 23:24pm
v2

The Control.ControlCollection (Controls property) has a Remove method too which you can use.
Clickety[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 9:23am    
This is better, my 5,
--SA
Just to remove it from the controls list without actually destroying the button object:
C#
Button myNewButton = new Button();
private void MyExistingButton_Click(object sender, EventArgs e)
{
  myNewButton.Text = "ClickMe";
  myNewButton.Location = new Point((Width - myNewButton.Width) / 2, (Height - myNewButton.Height) / 2);
  myNewButton.Click += new EventHandler(myNewButton_Click);
  Controls.Add(myNewButton);
}

void myNewButton_Click(object sender, EventArgs e)
{
  MessageBox.Show("Hello World!");
}

private void MyExistingButton2_Click(object sender, EventArgs e)
{
  if(Controls.Contains(myNewButton))
    Controls.Remove(myNewButton);
}
 
Share this answer
 
v3
Comments
ZPS769 18-Feb-11 8:31am    
Not exactly but After seeing these 3 answeres I figured out solution .
thanks to all of you
Sergey Alexandrovich Kryukov 18-Feb-11 9:30am    
You too, assume the the parent control is the form or a control declaring the methods you post here.
Even if this is the case, only due to the sloppiness of layout. Usually controls like that are put on same panels, etc. Just pointing to API would suffice.

Also, a note of naming conventions: names like "myNewButton_Click" come from the code auto-generated by the Designer. It does not mean they should be used: even though it is generated by Microsoft, such name violate Microsoft naming conventions, should always be renamed. But my best advice is never generate event handlers automatically, it does not accelerate development (but a source of many confusions), the easiest and supportable way is to use anonymouse delegates (since C# v.2).

(I did not vote.)
--SA
Espen Harlinn 20-Feb-11 9:24am    
Good reply :)
Abhinav S 22-Feb-11 0:26am    
Good answer. 5.
public Form1()
        {
            InitializeComponent();
            
        }
        Button myNewButton;

        private void MyExistingButton_Click(object sender, EventArgs e)
        {
            if (!(this.Controls.Contains(myNewButton)))
            {
                myNewButton = new Button();
                myNewButton.Text = "ClickMe";
                myNewButton.Location = new Point((Width - myNewButton.Width) / 2, (Height - myNewButton.Height) / 2);
                myNewButton.Click += new EventHandler(myNewButton_Click);
                Controls.Add(myNewButton);
            }
        }
        void myNewButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello World!");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.Controls.Contains(myNewButton))
            {
                this.Controls.Remove(myNewButton);
            }
        }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 9:31am    
You got it.
--SA
Lets do it step by step:
1.) Instead of declaring myNewButton as a local variable, make it global variable. (I would rather have created a class which would store my dynamic controls)
2.) Now in some other event (whatever it is), you can remove/delete it from the controls[^].

C#
if(Controls.Contains(myNewButton ))
{
  Controls.Remove(myNewButton );
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 9:22am    
Manas, just the code snipped would suffice.
You play with global/local variable or class to store controls is all in vain: this is not a real application anyway; and all button variable are declared in the form and visible at the point of the call; strange that you did not take care of the parent: the code looks like you're calling a parent's method. Why? you should not assume the parent is the Form.
--SA
If you want to remove all controls just use
Controls.Clear()
 
Share this answer
 

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