Instead of an array, you should use a list. I think when creating you do something like this:
List<button> buttons = new List<button>(); for( int i = 0; i < 20; i++ ){ Button b = new Button(); ... this.Controls.Add(button); buttons.Add(button); }
Then, to remove any button from the application, simply do:
this.Controls.Remove( buttons[i] ); buttons.RemoveAt(i);
Using this setting, to remove the last 15 buttons, try the following:
for( int i = 19; i > 4; i-- ){ this.Controls.Remove(buttons[i]); buttons.RemoveAt(i);
Do not forget that the cycle starts from the 20th element and works down, because if you delete an element inside the list, it means that all elements with a higher index will have an index shifted by 1.
Γyvind BrΓ₯then
source share