I have found that using the Bring to front & Send to Back functions on controls created at runtime does not work.

Does anyone know a way to solve this problem?
any help would be appriciated.

I'd revisit that assumption, there could be something else incorrect in your code. Consider this example.

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

        private void Form1_Load(object sender, EventArgs e)
        {
            int buttonID = 0;

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    int left = j * 20;
                    int top = i * 20;

                    Button button = new Button();
                    button.Text = buttonID++.ToString();
                    button.Click += new EventHandler(button_Click);
                    button.Top = top;
                    button.Left = left;
                    this.Controls.Add(button);
                }
            }
        }

        void button_Click(object sender, EventArgs e)
        {
            (sender as Button).BringToFront();
        }
    }

oh this helps alot thanks ;)

I have one more question, when you have a 2d array of labels how do you handle events for each one of them?

If there's nothing particularly unique about the way you want to handle them individually, you can do exactly as I did above: have each of them subscribe to the same event handler. The first parameter in your event handler (object sender) will refer to the label that raised the event. Cast the object to Label and work with it however you need to.

What if you want to handle two buttons differently but the other 10 the same. How would you differenciate buttons?

If you know what buttons you want to handle differently, program for that situation. I assume you have a reason for having those in the control array rather than explicitly defining them at design time.

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

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 12; i++)
            {
                Button button = new Button();

                if (i == 4) // why 4? don't know.
                    button.Click += new EventHandler(button4_Click);
                else if (i == 7) // ditto.
                    button.Click += new EventHandler(button7_Click);
                else 
                    button.Click += new EventHandler(button_Click);

                button.Text = i.ToString();
                button.Top = i * button.Height;
                this.Controls.Add(button);
            }
        }

        void button_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Standard button event handler.");
        }

        void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Special handler for button 4");
        }

        void button7_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Special handler for button 7");
        }
    }

oh wow i never thought of using the for loop counter to differenciate them.
Thanks alot! ;)

Hi Friends,
Wel i´am not sure about that way, becayse you will have an if() for each button
it would be nice if check this code

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

        private void Form1_Load(object sender, EventArgs e)
        {
            int buttonID = 0; 
            for (int i = 0; i < 10; i++) 
            { 
                for (int j = 0; j < 10; j++) 
                { 
                    int left = j * 20;
                    int top = i * 20; 
                    Button button = new Button();
                    button.Text = buttonID++.ToString();
                    button.Name = button.Text;
                    button.Click += new EventHandler(button_Click);
                    button.Top = top; 
                    button.Left = left;
                    this.Controls.Add(button); 
                } 
            }
        }

        void button_Click(object sender, EventArgs e) 
        {
            Button btnMyButton = (Button)sender;
            MessageBox.Show(btnMyButton.Name.ToString());
        }
    }

Regards from Perú

By jMosquera
Net Cell Continental

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.