1,105,384 Community Members

Creating a function that controls an array of buttons

Member Avatar
staticclass
Newbie Poster
9 posts since Feb 2011
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, guys
I'm new to this forum, and also kind of new to C#. I actually develop programs in C# for 2 years, but in a very amateur level.

Here is my question;
I'm trying to make a game like Block'd

I found the way to create an array of buttons, and colored them randomly, too. Below is the screencap of my program's debug and the code

And I have to make a function that controls any button clicked, and takes its index numbers, and check if it has the same color as its adjacent blocks etc.

I don't want to create 400 functions like
private void Button1_Click
private void Button2_Click
private void Button3_Click
...
private void Button400_Click

so what would be a convenient way to create such a function?

Note: I don't know what an EventHandler, or a sender is, if the answer involves these terms, please briefly explain what do they do, if you can.

Thanks in advance..

http://img40.imageshack.us/img40/9239/screencapo.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BlockdDeneme2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        public string s = Application.StartupPath + "\\pics\\";
        public string jpg = ".jpg";
        public Random r = new Random();
        public int MaxButtonNumberFurk = 20;
        public Button[,] Buttons = new Button[20,20];
        public int i,j;
        private void Form1_Load(object sender, EventArgs e)
        {
            label5.Visible = false;
            button1.Visible = false;
            int howmany = 15;
            CreateButtons(howmany, howmany);
        
            
        }

      
        public void ColorTheButtons(Button bttn) //1:green, 2:blue, 3:red , 4:yellow
        {
            if (bttn.ImageIndex == 1)
            {
                bttn.BackColor = Color.Green;
            }
            else if (bttn.ImageIndex == 2)
            {
                bttn.BackColor = Color.RoyalBlue;
            }
            else if(bttn.ImageIndex == 3)
            {
                bttn.BackColor = Color.OrangeRed;
            }
            else if(bttn.ImageIndex == 4)
            {
                bttn.BackColor = Color.Gold;
            }
            else
            {
                bttn.BackColor = Color.Black;
            }
        }
        public void CreateButtons(int NumX, int NumY)
        {

            this.Location = new Point(2, 2);
            this.Size = new Size((NumX+1) * 40-20,(NumY+1) * 40);

            int temp;
            for (i = 0; i < NumX; i++)
            {
                for(j=0;j<NumY;j++)
                {

                Buttons[i,j] = new Button();
                Buttons[i,j].Name = "ItemNum_" + i.ToString();
                Buttons[i,j].Location = new Point(40 * i, 40*j);
                Buttons[i,j].Size = new Size(40, 40);
                temp = (r.Next(4) + 1);
                Buttons[i, j].ImageIndex = temp;
                label5.Text += " " + temp.ToString();
               
                ColorTheButtons(Buttons[i, j]);
                
               
                Buttons[i,j].Visible = true;

                this.Controls.Add(Buttons[i,j]);

              
                
                }
                label5.Text += "\n";
            }
           

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }


    }
}
Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

When creating a new button, create a Click event with it:

public void CreateButtons(int NumX, int NumY)
        {
            this.Location = new Point(2, 2);
            this.Size = new Size((NumX + 1) * 40 - 20, (NumY + 1) * 40);

            int temp;
            for (int i = 0; i < NumX; i++)
            {
                for (int j = 0; j < NumY; j++)
                {

                    Buttons[i, j] = new Button();
                    Buttons[i, j].Name = "ItemNum_" + i.ToString();
                    Buttons[i, j].Location = new Point(40 * i, 40 * j);
                    Buttons[i, j].Size = new Size(40, 40);
                    temp = (r.Next(4) + 1);
                    Buttons[i, j].ImageIndex = temp;
                    label5.Text += " " + temp.ToString();
                    ColorTheButtons(Buttons[i, j]);
                    Buttons[i, j].Visible = true;

                    //CREATE A COMMON EVENT:
                    Buttons[i, j].Click += new EventHandler(Form1_Click);

                    this.Controls.Add(Buttons[i, j]);
                    //= Buttons .Controls.Find(Buttons[i,j].Name);

                }
                label5.Text += "\n";
            }
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            Button _button = sender as Button;
            //NOW YOU CAN CONTINUE WITH THE CODE...
            //_button HAS ALL THE PROPERTIES, ANS STUFF OF THE CLICKED BUTTON!  
            //FOR EXAMPLE:
            string name = _button.Text;
        }

And then you will have a common event handler for all the buttons.
Hope it helps,
Mitja

Member Avatar
staticclass
Newbie Poster
9 posts since Feb 2011
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

And then you will have a common event handler for all the buttons.
Hope it helps,
Mitja

Oh my god, I knew that the answer is that easy, but I just didn't know and couldn't find it at google =)

Thank you very much Mitja, I will mark this thread as solved, but in case I do have further questions, I hope you could be here..

Thanks again =)

Question Answered as of 3 Years Ago by Mitja Bonca
Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

Sure, anytime.
And thx. I am glad Ican help.
Cya
Mijta

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: