Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i created dynamic buttons using following code in run times using visual studio in c#. now i want to deleting some buttons that i created in run time.how can i do that

What I have tried:

Button dynamicButton = new Button();
dynamicButton.Location = new Point(300 + tx4, 200 + tx3);
dynamicButton.Height = nh;
dynamicButton.Width = nw;
dynamicButton.BackColor = Color.Gray;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Click += new EventHandler(DynamicButton_Click);
Controls.Add(dynamicButton);
Posted
Updated 23-Aug-16 10:24am

What your sample-code is showing is the dynamic creation of a Button.
After this creation the Button is added to the Controls-Collection of it's Host (perhaps the Form).
If you want to delete a Control you only need to remove it from the same Controls-Collection (Controls.Remove).
The Problem here perhaps is to identify the right Control for removing. For this it could be helpful to give each Control a Name - the Visual-Studio-Designer normally does this for you. If you create a Control dynamic you should mame it by your code ...
 
Share this answer
 
Comments
Mashi Bandara 23-Aug-16 3:58am    
do you know how to named that creating buttons to identify it for remove
Ralf Meier 23-Aug-16 6:52am    
If you want to Name the Button you should use the Name.Property of the Button (each Control has such a Property).
The C#-Code would be like this :
dynamicButton.Name = "mySelfCreatedButton_01"

So ... if you want to remove a special Control out of your Controls-Collection you could search it by it's Name and then remove it.

But ... if you had connected some Eventhandlers manually to this Control you should remove them first ...
Mashi Bandara 23-Aug-16 7:51am    
ok.. thank you
Maciej Los 23-Aug-16 16:14pm    
5ed!
Ralf Meier 24-Aug-16 1:00am    
Thank you, Maciej :)
 
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