Link to home
Start Free TrialLog in
Avatar of Marius0188
Marius0188

asked on

C# foreach iteration

Good day all,

I am not the worlds most experienced C# developer so please bear with me :)
Recently started to move projects over to .NET C#.

I have a very simple issue which I do not seem to understand.

I have a method that creates buttons and also another method that disposes them off.
But in the disposing method my foreach iteration seems to miss the last control everytime.

I am not sure as to why this would happen.
Two buttons gets created in dynamically no problem.
When running the DisposeButtons() method all except one button gets removed.

Please see my code - been on loooong day so perhaps I am overlooking something here.

Thanks in advance :)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadButtons();
        }

        private void LoadButtons()
        {
            var top = 20;
            var verticalspace = 5;

            for (int x = 0; x < 2; x++)
            {
                top += top + verticalspace;

                Button b = new Button();
                b.Text = "button_" + x.ToString();
                b.Top = top;
                b.Left = 20;
                b.Name = "button_" + x.ToString();
                this.Controls.Add(b);
            }
        }

        private void DisposeButtons()
        {
            foreach (Control c in this.Controls)
            {
                if (c is Button) {
                    this.Controls.Remove(c);
                }
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                LoadButtons();
            }
            else
            {
                DisposeButtons();
            }
        }
    }
}

Open in new window

Avatar of QPR
QPR
Flag of New Zealand image

Could it be you have nested controls?
Have a look here

http://stackoverflow.com/questions/5794699/change-all-buttons-on-a-form
Avatar of Marius0188
Marius0188

ASKER

Hi QPR,

Thanks for the reply.
If I understand correctly nest control = controls within controls?

Nope - well not according to me.
All the buttons are explicitly added to the form this().

:)
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could also add those dynamic buttons to a class level List<Button>, then iterate over that and work with them that way.
Aaaah - I know it.
Something so simple. How would I have not thought about it.

Great Idle_Mind and thanks a lot for this.
I need sleep :)

Points awarded to you....