i am making a program need 128 button
when u click on button color of button change to red
when you click it a gain it back to normal color
is there a way to code one button and the other take the same function
thank at all

Button[] btnsAry = (Button[])FindControlsOfType(/*this*/ form1, typeof(Button));
                foreach (Button btn in btnsAry)
                    btn.BackColor = Color.Red;

Or, you could use a list (List<>):

List<Control> btnList = ListControlsOfType(/*this*/ form1, typeof(Button));
                foreach (Button btn in btnList)
                    btn.BackColor = Color.Red;

thanks very much
the compiler give me a message
The name 'FindControlsOfType' does not exist in the current context

thanks very much
the compiler give me a message
The name 'FindControlsOfType' does not exist in the current context

Sorry about that. I just realized that I had copied those methods from somewhere into a code file I have. I searched the forum to see if I found them here, but I got no matches. Anyway, I'm going to repost them since I can't locate the link to direct you too, but it's pretty straight-forward stuff that also uses recursion to search child controls:

public List<Control> ListControlsOfType(Control ctrl, Type t)
            {
                List<Control> ctrlList = new List<Control>();
                foreach (Control c in ctrl.Controls)
                {
                    // add this control if of type specified...
                    if ((c.GetType() == t) || c.GetType().IsSubclassOf(t))
                        ctrlList.Add(c);

                    // search and add all child controls too...
                    foreach (Control childCtrl in c.Controls)
                        ctrlList.AddRange(ListControlsOfType(childCtrl, t));
                }
                return ctrlList;
            }

            // use ListControlsOfType instead!!!
            public Array FindControlsOfType(Control ctrl, Type t)
            {
                System.Collections.ArrayList result = new System.Collections.ArrayList();
                foreach (Control c in ctrl.Controls)
                {
                    if ((c.GetType() == t) || c.GetType().IsSubclassOf(t))
                        result.Add(c);
                    foreach (Control childCtrl in c.Controls)
                        result.AddRange(FindControlsOfType(childCtrl, t));
                }
                return result.ToArray(t);
            }

EDIT: To whomever wrote those methods, my apologies for not giving you credit.:$

Edited 7 Years Ago by DdoubleD: n/a

Think about common event handler for buttons,

...
  private void Form1_Load(object sender, EventArgs e)
        {
            button1.Click += new EventHandler(commonHandler);
            button2.Click += new EventHandler(commonHandler);
            button3.Click += new EventHandler(commonHandler);
            button4.Click += new EventHandler(commonHandler);
        }

        void commonHandler(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (btn.BackColor == this.BackColor)
                btn.BackColor = Color.Red;
            else
                btn.BackColor = this.BackColor;
        }
  ...

After rereading your post, it occurs to me that adatapost has probably hit more on what you are asking for. Just combine the control iteration examples with his "common" event wiring example and you have your answer.

thanks very much (adatapost)
your post was vey useful
and u DoubleD thank you
but only one think
i have 128 button so i must write
button1.Click += new EventHandler(commonHandler);
for each one
is there another way
and sorry for bother

i create a solve

private void Form1_Load(object sender, EventArgs e)
        {
            List<Control> btnList = ListControlsOfType(this, typeof(Button));
            foreach (Button btn in btnList)
                    btn.Click += new EventHandler(commonHandler);
            //button11.Click += new EventHandler(commonHandler);
            //button12.Click += new EventHandler(commonHandler);
            //button13.Click += new EventHandler(commonHandler);
            //button14.Click += new EventHandler(commonHandler);
            //button15.Click += new EventHandler(commonHandler);
        }

        void commonHandler(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (btn.BackColor == this.BackColor)
                btn.BackColor = Color.Red;
            else
                btn.BackColor = this.BackColor;
        }
This question has already been answered. Start a new discussion instead.