1,105,399 Community Members

Bring to front & Send to Back on Control Arrays

Member Avatar
toko
Junior Poster
104 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
apegram
LINQ!
550 posts since Jan 2010
Reputation Points: 302 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 10 [?]
Team Colleague
 
0
 

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();
        }
    }
Member Avatar
toko
Junior Poster
104 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
apegram
LINQ!
550 posts since Jan 2010
Reputation Points: 302 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 10 [?]
Team Colleague
 
0
 

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.

Member Avatar
toko
Junior Poster
104 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
apegram
LINQ!
550 posts since Jan 2010
Reputation Points: 302 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 10 [?]
Team Colleague
 
0
 

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");
        }
    }
Member Avatar
toko
Junior Poster
104 posts since Aug 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 4 Years Ago by apegram
Member Avatar
jmosquera1987
Newbie Poster
2 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article