how to change the property of particular control in array of control

Posted on 2006-06-05
Last Modified: 2010-04-16
Hi Experts,

I have created on project in C#.
Which creates one or more runtime time button.i.e. array of button controls.
According to button pressed the property should be changed for that particular button.
and I want to create one event to handle this change.
I have subscribeed same event to all control buttons.
But unfortunately it affects on only one button not any other button.
means how to code for control array in event.

can you give me hint how to do this.

Thanks in advance.
Question by:avinash_takale
    LVL 85

    Accepted Solution

    Cast the sender parameter to a button...

    Here is a simple example:

            private void button1_Click(object sender, EventArgs e)
                Button btn;
                for (int i = 0; i < 5; i++)
                    btn = new Button();
                    btn.Text = "Button" + i.ToString();
                    btn.Location = new Point(i * btn.Width, 50);
                    btn.Click += new EventHandler(btn_Click);

            void btn_Click(object sender, EventArgs e)
                Button btn = (Button)sender;
                btn.Text = "Clicked";
                btn.BackColor = Color.Red;

    Author Comment

    Thank a lots
    I really appriciated.
    Its working fine,  

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now