hi guys please help me

i want to know how i can pass an i ,j argumaents to the function abc so that i can do oprations on i, j in function abc

Button[,] b = new Button[8, 8];

        void drawchessboard()
            int x=60, y=10,z=0;
            for (int i = 0; i < 8; i++)
                for (int j = 0; j < 8; j++)
                    b[i, j] = new Button();
                    b[i, j].Name = "b" + Convert.ToString(i) + Convert.ToString(j);
                    b[i, j].Text = " ";
                    b[i, j].Width = 80;
                    b[i, j].Height = 80;
                    b[i, j].FlatStyle = FlatStyle.Flat;
                    b[i, j].Location = new Point(x,y);
                    if (z == 0)
                    {   b[i, j].BackColor = Color.White;
                        z = 1;  }
                    {   b[i, j].BackColor = Color.DarkGray;
                        z = 0;  }
                    b[i, j].Click += new EventHandler(abc);
                    Button button = new Button();
                    x = x + 81;
                y = y + 81;
                x = 60;
                { z = 0;}
                { z = 1; }
        void drawpieces()
            for (int j = 0; j < 8; j++)
                b[1, j].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory+"/black_pawn.png"); 
                b[6, j].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory+"/white_pawn.png");
            b[0, 0].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_rook.png");
            b[0, 7].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_rook.png");
            b[0, 1].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_knight.png");
            b[0, 6].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_knight.png");
            b[0, 2].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_bishop.png");
            b[0, 5].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_bishop.png");
            b[0, 3].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_queen.png");
            b[0, 4].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_king.png");
            b[7, 0].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_rook.png");
            b[7, 7].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_rook.png");
            b[7, 1].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_knight.png");
            b[7, 6].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_knight.png");
            b[7, 2].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_bishop.png");
            b[7, 5].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_bishop.png");
            b[7, 3].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_queen.png");
            b[7, 4].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_king.png");


        void abc(object sender, EventArgs e)

        private void Form1_Load(object sender, EventArgs e)

Edited by peter_budo: Do not use CODE SNIPPET to posts questions! Use Discussion Thread

5 Years
Discussion Span
Last Post by skatamatic

I see that your function abc locks like a event , you add a event to a control this way
<control name>.event += new new EventHandler(<event function>).
In your case it would be:

b[i,j].Click += new EventHandler(abc);

hello sir

actually i m trying to make a chess engine

thanks for help but it didnt work out for me bcoz i just want to know which button is selected from 8x8 grid which is possible by knowing the values of i, j in grid and after that i can apply oprations on i,j for further actions

so i want to know how i will get a value of i,j in function abc.
please read the code carefully.

thanks in advance


Your abc click handler would loo something like

private void abc_Click(object sender, EventArgs e)
//code here

Now sender.Name will give you something like "bij"(so extract the i and j from there), you could also use the Location property in this case or the Tag property, to do what you want to do. Success! :)


You could use the .Tag property of each button to give it a coordinate. Then subscribe all the buttons' events to one event, and use this coordinate to identify the button.

in drawchessboard:

b[i,j].Tag = new Point(i, j);
private void abc(object sender, EventArgs e)
   Point ButtonCoordinate = (Point)(((Button)sender).Tag);

A bit ugly parenthesis wise, but that's just to get it all on one line. It could be written like this:

private void abc(object sender, EventArgs e)
   Button CallingButton = (Button)sender;
   Point ButtonCoordinate = (Point)(CallingButton.Tag);

A side note, it looks like line 25 is totally useless and can be removed.

Edited by skatamatic: n/a


hi sir thanks for help

i wanna know how i can return the name of image of the button


hi sir thanks for help

i wanna know how i can return the name of image of the button

The .Image property doesn't contain a name, but it does have a .Tag property. You can use this property when assigning the image to the button to hold a string, which you can then use to get this data, just like you did with the coordinates on the button itself. You could also keep an internal list of images with an assosiated name (a custom image class of sorts). When you assign the image to the buttons, use a reference to the image in this list rather than a 'new' image. Then you can search this list for an identical reference to the .Image property of the button, and get the associated name that way. The first method is propably easier though...

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.