can anybody tell me how can i create array of buttons so that i can give same code to every button

Button[] btnArr = new Button[COUNT]; //count is the amount of buttons this array can hold.

//Add/Change a button:
btnArr[indx] = IAmButton; //indx is the position, IAmButton is the button you are adding.

//Get a button:
IAmButton = btnArr[indx];

//Get a specific button. Not so sure about this one...
TempButton = btnArr.Controls.Find(name); //name is a string, that you named the button you looking for.

Ok. Now that method was for windows forms. Depending on what your project is and what this code you are applying to the buttons is for, a WPF application might make that process a bit more clean and managable.

"Give same Code ??"
Do you mean giving them all the same event handler ? then let the event handler determine the button that was used ?

yes, actually i have to develop a array of buttons that have exactly the same working but thay will send message to diff pc

"Give same Code ??"
Do you mean giving them all the same event handler ? then let the event handler determine the button that was used ?

Okay, then if I can assume that the buttons already exist on the form, then simply have all the buttons point to the same event handler, and examine the object sender to determine which button was pressed.

Button btn = sender as Button;
Now btn is the button thas was pressed, and you can take whatever action you want on it.

If the button has a payload in the Tag property you can use that to determine how to send &*^%$ to the other PC.

Summary: The form contains the array of buttons, they all can share the same event handler, and perform actions based on some criteria of the button properties.

Does this help ?

Jerry

Member Avatar for Moirae

You helped me too, thanks a lot!
I'm a bit newbie in C# and I took it as a optional course at my college. Now I have to make a program instead of exam and I find it difficult. My teacher is great but I'm afraid to ask him for help because I'm in a beginning of a project, and he will be disappointed. So I will ask you a number of stupid questions :) :)
Where do I add this button array, I mean where do I put the code?
Into Form or into Program class that automatically generates??
Thanks

Into the form class.

Member Avatar for Moirae

Thank you very much.:) I put it in Form1_Load region, and I have multidimensional array (like 5x5 matrix), but I can't add buttons on the form, if I use Controls.Add it adds only one button, and if i use Controls.AddRange program gives me an error :'(

Please create a new thread and ask questions there -- you should not append your questions to someone elses thread. It would make Daniweb a mess if we did that and nobody likes a mess :)

I look forward to seeing you post!

int count;
        int i = 0;
        int x = 5;
        int y=10;

        Panel[] pnl_new_ = new Panel[10];
        PictureBox[] usr_pic_ = new PictureBox[10];
        Label[] lbl_username_ = new Label[10];
        Label[] lbl_account_type_ = new Label[10];
             
        private void button1_Click(object sender, EventArgs e)
        {
            i = Convert.ToInt32(txt_num.Text);            
          
            //string msg = count.ToString();
            //MessageBox.Show(msg);

            //if (count >= 0 && count < 9)
            //{
            for (count = 0; count < i; )
            {
                //if (count < 3)
                //{
                    Panel pnl = new Panel();

                    pnl.Location = new Point(x, y);
                    pnl.Size = new Size(238, 97);
                    pnl.BackColor = Color.LightGray;    
                    pnl.Cursor = System.Windows.Forms.Cursors.Hand;

                    //pnl.MouseLeave += new EventHandler(pnl_MouseLeave);
                    //pnl.MouseHover += new System.EventHandler(pnl_MouseHover);

                    pnl_new_[count] = pnl;
                    pnl_new_[count].MouseHover+=new EventHandler(Manage_Another_Account_frm_MouseHover);
                    grp_account.Controls.Add(pnl_new_[count]);

                    PictureBox pic = new PictureBox();

                    pic.Location = new Point(12, 12);
                    pic.Size = new Size(70, 74);
                    pic.BackColor = Color.Green;

                    usr_pic_[count] = pic;
                    pnl_new_[count].Controls.Add(usr_pic_[count]);

                    Label lbl_name = new Label();

                    lbl_name.Location = new Point(94, 21);
                    lbl_name.Size = new Size(122, 18);
                    lbl_name.Text = "Sunita Shrestha";
                    lbl_name.ForeColor = Color.Green;
                    lbl_name.Font = new Font("Tahoma", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                    lbl_username_[count] = lbl_name;
                    pnl_new_[count].Controls.Add(lbl_username_[count]);

                    Label lbl_actype = new Label();

                    lbl_actype.Location = new Point(94, 47);
                    lbl_actype.Size = new Size(122, 18);
                    lbl_actype.Text = "Administrator";
                    lbl_actype.ForeColor = Color.Black;
                    lbl_actype.Font = new Font("Tohama", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                    lbl_account_type_[count] = lbl_actype;
                    pnl_new_[count].Controls.Add(lbl_account_type_[count]);

                //}

                count = count + 1;
                x += 257;
            }
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.