Hi...!
I was working on a basic chess game it shows the possible move for each of the piece that you've selected.
I uploaded the file in This link :

When you click on a button form title is the postion of the button you've selected but i don't know how to do somthing that the button that i choose Change color... is there any idea?! i don't know that piece of code...
if this Description is not enough tell me to describ more...
Thanks a lot...

If you need to change the color of a button (at least this is what I understood) you can use the button.BackColor = Color.Green

If you needed something else please give more details.

This Is The Code:

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 Chess1
{
    public partial class Form1 : Form
    {
        public class Position
        {
            private int X, Y;
            public Position(int x,int y)
            {
                X = x;
                Y = y;
            }

            public Position() { }

            public int getX()
            {
                return X;
            }
            public int getY()
            {
                return Y;
            }
        }
        public class SelectedPiece
        {
            private string Piece;
            public SelectedPiece()
            {
            }
            public void getPeice(int i)
            {
                if (i == 1) Piece = "king";
                if (i == 2) Piece = "queen";
                if (i == 3) Piece = "bishop";
                if (i == 4) Piece = "rook";
                if (i == 5) Piece = "knight";
                if (i == 6) Piece = "pawn";

            }
            public string returnpeice()
            {
                if ("pawn".Equals(Piece)) return "pawn";
                if ("queen".Equals(Piece)) return "queen";
                if ("bishop".Equals(Piece)) return "bishop";
                if ("king".Equals(Piece)) return "king";
                if ("knight".Equals(Piece)) return "knight";
                if ("rook".Equals(Piece)) return "rook";
                else return "123";
            }
        }
        public class Pieces
        {
            private string Peice;
            private int x,y;
            public Pieces(int X, int Y, string P)
            {
                x = X;
                y = Y;
                Peice = P;
            }

        }
        public Form1()
        {
            InitializeComponent();
        }
        SelectedPiece Sp = new SelectedPiece();
        Position position = new Position();
        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            for (int i = 1; i <= 8; i++)
            {
                for (int j = 1; j <= 8; j++)
                {
                    Button btn = new Button();
                    btn.Width = 70;
                    btn.Height = 70;
                    btn.Left=( i ) * (btn.Width + 5);
                    btn.Top = (j ) * (btn.Width + 5);
                    btn.Text = i + "," + j;
                    Controls.Add(btn);
                    Position temp = new Position(i, j);
                    btn.Tag = temp;

                    btn.Click+=new EventHandler(btn_Click);
                }
            }
        }

        private void btn_Click(object sender, EventArgs e)
        {
            Pieces peice = new Pieces(position.getX(), position.getY(), Sp.returnpeice());
            Position pos = new Position();
            pos = (Position)((Button)sender).Tag;

            this.Text =Convert.ToString (pos.getX()) +Convert.ToString(pos.getY());
        }
        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (rbtnKing.Checked)
            {
                Sp.getPeice(1);
            }
            if (rbtnQueen.Checked)
            {
                Sp.getPeice(2);
            }
            if (rbtnBishop.Checked)
            {
                Sp.getPeice(3);
            }
            if (rbtnRook.Checked)
            {
                Sp.getPeice(4);
            }
            if (rbtnKnight.Checked)
            {
                Sp.getPeice(5);
            }
            if (rbtnPawn.Checked)
            {
                Sp.getPeice(6);
            }
            this.Text = Sp.returnpeice();
        }

    }
}

in private void btn_Click(object sender, EventArgs e) i want that button in specific postion change color but with use of Tag Command how could i do that?!

Since I'm not able to fully understand what you are trying to do... here's what I can give you

private void btn_Click(object sender, EventArgs e)
Pieces peice = new Pieces(position.getX(), position.getY(), Sp.returnpeice());

position is a new Position() so getX and getY always return 0 and 0 (even though you're not using it afterwards)

As I said in my previous post you can change color with (sender as Button).BackColor = Color.Green;. I don't know what you mean by using Tag command. From your code the Tag is just a x/y position on the window. Why do you want to relate to that position since the sender can be cast as button and colored?

Thanks...
for example you selected the pawn and you select the 2,4 postion on the screen i want that buttons in postions that a pawn possible to move get colored how can i do that?!

I made some changes to your code to get you started. First of all you have to think of a chess board/piece. A piece has directions of movement, movement pattern and maximum moves. I added a class that simulates these three attributes of a chess piece. So once you load the program, select one piece and it will give it the right attributes. Then if you click on a chess board button it shows where it can go.

  • the coloring part is pretty much done (except the knight)
  • I have replaced the positions you are using as Tags with button's Left an Top properties which can then be used to get the other buttons based on their position

Now for the not so good part for you :)
- I have only defined king and queen movements but this should be enough as an example. You have to define the other pieces
- the movement class has a ComplexMovement... you could use this for the Knight piece which has a complex pattern... it's your job to figure out how to simulate it

Let me know if you have any questions

 public partial class Form1 : Form
    {
        public Pieces SelectedPiece;

        public class Position
        {
            private int X, Y;
            public Position(int x,int y)
            {
                X = x;
                Y = y;
            }

            public Position() { }

            public int getX()
            {
                return X;
            }
            public int getY()
            {
                return Y;
            }
        }
        public class Pieces
        {
            public string PieceName { get; set; }
            public Position StartPosition { get; set; }
            public Movement Moves { get; set; }
        }
        public enum Direction
        {
            Up,
            Down,
            Left,
            Right,
            UpRight,
            UpLeft,
            DownRight,
            DownLeft
        }
        public class Movement
        {
            public List<Direction> DirectionsList = new List<Direction>();
            public List<Direction> ComplexDirection = new List<Direction>();
            public int MaxSteps { get; set; }
        }
        public Form1()
        {
            InitializeComponent();
        }

        Position position = new Position();

        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            for (int i = 1; i <= 8; i++)
            {
                for (int j = 1; j <= 8; j++)
                {
                    Button btn = new Button();
                    btn.Width = 70;
                    btn.Height = 70;
                    btn.Left=( i ) * (btn.Width + 5);
                    btn.Top = (j ) * (btn.Width + 5);
                    btn.Text = i + "," + j;
                    Controls.Add(btn);
                    Position temp = new Position(btn.Left, btn.Top);
                    btn.Tag = temp;
                    btn.BackColor = Color.Gray;
                    btn.Click+=new EventHandler(btn_Click);
                }
            }
        }

        private void btn_Click(object sender, EventArgs e)
        {
            Position pos = new Position();
            pos = (Position)((Button)sender).Tag;
            this.Text = Convert.ToString(pos.getX()) + Convert.ToString(pos.getY());
            (sender as Button).BackColor = Color.Green;
            for (int i = 1; i <= SelectedPiece.Moves.MaxSteps; i++)
            {
                foreach (var direction in SelectedPiece.Moves.DirectionsList)
                {
                    switch(direction)
                    {
                        case Direction.Up:
                            var foundButtonUp = this.GetChildAtPoint(new Point(pos.getX(), pos.getY() - (75 * i)));
                            if (foundButtonUp != null)
                            {
                                (foundButtonUp as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.Down:
                            var foundButtonDown = this.GetChildAtPoint(new Point(pos.getX(), pos.getY() + (75 * i)));
                            if (foundButtonDown != null)
                            {
                                (foundButtonDown as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.Left:
                            var foundButtonLeft = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY()));
                            if (foundButtonLeft != null)
                            {
                                (foundButtonLeft as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.Right:
                            var foundButtonRight = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY()));
                            if (foundButtonRight != null)
                            {
                                (foundButtonRight as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.UpLeft:
                            var foundButtonUpLeft = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY() - (75 * i)));
                            if (foundButtonUpLeft != null)
                            {
                                (foundButtonUpLeft as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.UpRight:
                            var foundButtonUpRight = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY() - (75 * i)));
                            if (foundButtonUpRight != null)
                            {
                                (foundButtonUpRight as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.DownLeft:
                            var foundButtonDownLeft = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY() + (75 * i)));
                            if (foundButtonDownLeft != null)
                            {
                                (foundButtonDownLeft as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.DownRight:
                            var foundButtonDownRight = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY() + (75 * i)));
                            if (foundButtonDownRight != null)
                            {
                                (foundButtonDownRight as Button).BackColor = Color.Green;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        private void btnSelect_Click(object sender, EventArgs e)
        {
            //reset all button colors on grid
            foreach (var ctrl in this.Controls)
            {
                if ((ctrl as Button) != null)
                {
                    (ctrl as Button).BackColor = Color.Gray;
                }
            }
            SelectedPiece = new Pieces();
            if (rbtnKing.Checked)
            {
                SelectedPiece.PieceName = "King";
                Movement pieceMovement = new Movement();
                pieceMovement.DirectionsList.Add(Direction.Down);
                pieceMovement.DirectionsList.Add(Direction.Up);
                pieceMovement.DirectionsList.Add(Direction.Left);
                pieceMovement.DirectionsList.Add(Direction.Right);
                pieceMovement.MaxSteps = 1;
                SelectedPiece.Moves = pieceMovement;
            }
            if (rbtnQueen.Checked)
            {
                SelectedPiece.PieceName = "Queen";
                Movement pieceMovement = new Movement();
                pieceMovement.DirectionsList.Add(Direction.Down);
                pieceMovement.DirectionsList.Add(Direction.Up);
                pieceMovement.DirectionsList.Add(Direction.Left);
                pieceMovement.DirectionsList.Add(Direction.Right);
                pieceMovement.DirectionsList.Add(Direction.UpLeft);
                pieceMovement.DirectionsList.Add(Direction.UpRight);
                pieceMovement.DirectionsList.Add(Direction.DownLeft);
                pieceMovement.DirectionsList.Add(Direction.DownRight);
                pieceMovement.MaxSteps = 8;
                SelectedPiece.Moves = pieceMovement;
            }
            if (rbtnBishop.Checked)
            {
                //Sp.getPeice(3);
            }
            if (rbtnRook.Checked)
            {
                //Sp.getPeice(4);
            }
            if (rbtnKnight.Checked)
            {
                //Sp.getPeice(5);
            }
            if (rbtnPawn.Checked)
            {
                //Sp.getPeice(6);
            }
            this.Text = SelectedPiece.PieceName;
        }

    }

Thanks So So much... :)
would you describe this part :

 public string PieceName { get; set; }
            public Position StartPosition { get; set; }
            public Movement Moves { get; set; }

and this part:

 public List<Direction> DirectionsList = new List<Direction>();
            public List<Direction> ComplexDirection = new List<Direction>();
            public int MaxSteps { get; set; }
  1. part this is a piece object used when you select one from the radio button list. There is a global Pieces called "SelectedPiece" which gets initialized when you click the select button. The piece has a name (king, queen...), a Movement object (which in turn has the properties from the 2 part you asked about) and a StartingPosition (which i did not use eventually :))

  2. part these are the properties of a movement. It has a list of directions (up, down...) where a piece can move, a maximum number of steps it can take (1 for king) and a complex direction which you can use to simulate the knight movement (2 verticaly, 1 horizontaly).

The code is pretty much self explanatory... you have one chess piece which has a movement option. the movement has direction and max limit.

Check out btn_selectClick to see how I defined the king and queen.

How Could i do that With Pieces On The board and the shows the possible move of each piece?!

What do you mean by "Pieces On The board"? Like simulate a chess game where the chess piecess are actually visible?

Yes The Chess pieces on board you select a piece and shows where that piece can go...

Same code I provided goes for showing movements. You just need to arrange the pieces (you could even have a graphical representation of a piece - image) and get the selected piece on button press instead of select button click.

in this line you set a new object of class Movment?!

public Movement Moves { get; set; }

This line actually tells you that the Piece object has a Moves property of type Movement. It does not contain anything until you explicitly set it.

Pieces p = new Pieces();
Movement move = new Movement();
p.Moves = move;

Then you can set the Movement properties for that Pieces.

Read the comment above.

Whenever you get stuck... take a deep breath and think of it logically. It may seem hard at first but when you'll do it the first time you'll see how easy it is.

Think of this line like this:

  • SelectedPiece - what's that... check where SelectedPiece is declared
  • oh... it's an object of type Pieces
  • let's check what particularities the object Pieces has
  • this object has moves, name...
  • the moves property is of type Movement
  • let's check what particularities the object Movement has
  • and so on...
  • you'll eventually get to the correct conclusion

Ok I work on it but i have a problem... when i click on a piece it shows nothing what is wrong with it?!

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 Chess2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public class Position
        {
            private int X, Y;
            public Position(int x, int y)
            {
                X = x;
                Y = y;
            }
            public Position() { }
            public int getX()
            {
                return X;
            }
            public int getY()
            {
                return Y;
            }

        }
        public class Piece
        {
            public string name
            {
                get;
                set;
            }
            public Movement Moves
            {
                get;
                set;
            }

        }
        Piece Selectedpiece = new Piece();
        public enum Direction
        {
            Up,
            Down,
            Left,
            Right,
            UpRight,
            UpLeft,
            DownRight,
            DownLeft,
            KnightRightUp,
            KnightRightDown,
            KnightLeftUp,
            KnightLeftDown,
            KnightDRightUp,
            KnightDRightDown,
            KnightDLeftUp,
            KnightDLeftDown
        }
        public class Movement
        {
            public int Maxsteps
            {
                get;
                set;
            }
            public List<Direction> DirectionsList = new List<Direction>();
        }
        int p = 1;
        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            for (int i = 1; i <= 8; i++)
            {
                for (int j = 1; j <= 8; j++)
                {
                    Button btn = new Button();
                    btn.Width = 70;
                    btn.Height = 70;
                    btn.Left = (i) * (btn.Width + 5);
                    btn.Top = (j) * (btn.Width + 5);
                    btn.Text = i + "," + j;
                    Controls.Add(btn);
                    Position temp = new Position(btn.Left, btn.Top);
                    btn.Tag = temp;

                        btn.BackColor = Color.Gray;

                        btn.Click += new EventHandler(btn_Click);
                        p++;
                }


            }
        }
        int kingrem = 2;
        int queenrem = 2;
        int bishoprem = 4;
        int pawnrem = 16;
        int knightrem = 4;
        int rookrem = 4;
        int y = 0;
        private void btn_Click(object sender, EventArgs e)
        {
            y++;

            if (rbtnKing.Checked)
            {
                (sender as Button).Text = "King";
                (sender as Button).Enabled = false;
                (sender as Button).BackColor = Color.Azure;
                kingrem--;
                if (kingrem == 0)
                {
                    rbtnKing.Checked = false;
                    rbtnKing.Enabled = false;   
                }
                rtxtStatus.Text =rtxtStatus.Text+ "King Remaining :" + kingrem + Environment.NewLine;

            }
            if (rbtnBishop.Checked)
            {
                (sender as Button).Text = "bishop";
                (sender as Button).Enabled = false;
                (sender as Button).BackColor = Color.Azure;
                bishoprem--;
                if (bishoprem == 0)
                {
                    rbtnBishop.Enabled = false;
                    rbtnBishop.Checked = false;
                }
                rtxtStatus.Text = rtxtStatus.Text + "Bishop Remaining:" + bishoprem + Environment.NewLine;

            }
            if (rbtnQueen.Checked)
            {
                (sender as Button).Text = "queen";
                (sender as Button).Enabled = false;
                (sender as Button).BackColor = Color.Azure;
                queenrem--;
                if (queenrem == 0)
                {
                    rbtnQueen.Enabled = false;
                    rbtnQueen.Checked = false;
                }
                rtxtStatus.Text = rtxtStatus.Text + "Queen Remaining:" + queenrem + Environment.NewLine;
            }
            if (rbtnRook.Checked)
            {
                (sender as Button).Text = "rook";
                (sender as Button).Enabled = false;
                (sender as Button).BackColor = Color.Azure;
                rookrem--;
                if (rookrem == 0)
                {
                    rbtnRook.Enabled = false;
                    rbtnRook.Checked = false;
                }
                rtxtStatus.Text = rtxtStatus.Text + "Rook Remaining:" + rookrem + Environment.NewLine;
            }
            if (rbtnKnight.Checked)
            {
                (sender as Button).Text = "knight";
                (sender as Button).Enabled = false;
                (sender as Button).BackColor = Color.Azure;
                knightrem--;
                if (knightrem == 0)
                {
                    rbtnKnight.Enabled = false;
                    rbtnKnight.Checked = false;
                }
                rtxtStatus.Text = rtxtStatus.Text + "Knight Remaining:" + knightrem + Environment.NewLine;
            }
            if (rbtnPawn.Checked)
            {
                (sender as Button).Text = "pawn";
                (sender as Button).Enabled = false;
                (sender as Button).BackColor = Color.Azure;
                pawnrem--;
                if (pawnrem == 0)
                {
                    rbtnPawn.Enabled = false;
                    rbtnPawn.Checked = false;
                }
                rtxtStatus.Text = rtxtStatus.Text + "Pawn Remaining:" + pawnrem + Environment.NewLine;
            }
            if (kingrem == 0 && queenrem == 0 && bishoprem == 0 && knightrem == 0 && pawnrem == 0 && rookrem == 0)
            {
                foreach(var ctrl in this.Controls)
                {
                    if ((ctrl as Button) != null)
                    {
                        (ctrl as Button).Enabled = true;
                    }
                }
                Piece SelectedPiece = new Piece();
                if((sender as Button).Text=="King")
                {
                    Button btn = new Button();
                    Movement pieceMovement = new Movement();
                    pieceMovement.DirectionsList.Add(Direction.Down);
                    pieceMovement.DirectionsList.Add(Direction.Up);
                    pieceMovement.DirectionsList.Add(Direction.Left);
                    pieceMovement.DirectionsList.Add(Direction.Right);
                    pieceMovement.DirectionsList.Add(Direction.UpLeft);
                    pieceMovement.DirectionsList.Add(Direction.UpRight);
                    pieceMovement.DirectionsList.Add(Direction.DownLeft);
                    pieceMovement.DirectionsList.Add(Direction.DownRight);
                    pieceMovement.Maxsteps = 1;
                    SelectedPiece.Moves = pieceMovement;
                    btn.Click+=new EventHandler(btn_PieceClick);
                    SelectedPiece.name = "King";

                }

            }
            this.Text =Convert.ToString (y);
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            Piece SelectedPiece = new Piece();
            if (rbtnKing.Checked)
            {

            }
        }
        private void btn_PieceClick(object sender, EventArgs e)
        {
            Position pos = new Position();
            pos = (Position)((Button)sender).Tag;
            for (int i = 1; i < Selectedpiece.Moves.Maxsteps; i++)
            {
                foreach (var direction in Selectedpiece.Moves.DirectionsList)
                {
                    switch (direction)
                    {
                        case Direction.Up:
                            var foundbuttonup = this.GetChildAtPoint(new Point(pos.getX(), pos.getY() - (75 * i)));
                            if (foundbuttonup != null || foundbuttonup.Enabled == true)
                            {
                                this.Text = "king up worked!";
                                (foundbuttonup as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.Down:
                            var foundButtonDown = this.GetChildAtPoint(new Point(pos.getX(), pos.getY() + (75 * i)));
                            if (foundButtonDown != null)
                            {
                                (foundButtonDown as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.Left:
                            var foundButtonLeft = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY()));
                            if (foundButtonLeft != null)
                            {
                                (foundButtonLeft as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.Right:
                            var foundButtonRight = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY()));
                            if (foundButtonRight != null)
                            {
                                (foundButtonRight as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.UpLeft:
                            var foundButtonUpLeft = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY() - (75 * i)));
                            if (foundButtonUpLeft != null)
                            {
                                (foundButtonUpLeft as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.UpRight:
                            var foundButtonUpRight = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY() - (75 * i)));
                            if (foundButtonUpRight != null)
                            {
                                (foundButtonUpRight as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.DownLeft:
                            var foundButtonDownLeft = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY() + (75 * i)));
                            if (foundButtonDownLeft != null)
                            {
                                (foundButtonDownLeft as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.DownRight:
                            var foundButtonDownRight = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY() + (75 * i)));
                            if (foundButtonDownRight != null)
                            {
                                (foundButtonDownRight as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightRightUp:
                            var foundbuttonknightrightup = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY() - ((75) * (2) * (i))));
                            if (foundbuttonknightrightup != null)
                            {
                                (foundbuttonknightrightup as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightRightDown:
                            var foundbuttonKnightRightDown = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY() + ((75) * (2) * (i))));
                            if (foundbuttonKnightRightDown != null)
                            {
                                (foundbuttonKnightRightDown as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightLeftUp:
                            var foundbuttonKnightLeftUp = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY() - ((75) * (2) * (i))));
                            if (foundbuttonKnightLeftUp != null)
                            {
                                (foundbuttonKnightLeftUp as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightLeftDown:
                            var foundbuttonKnightLeftDown = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY() + ((75) * (2) * (i))));
                            if (foundbuttonKnightLeftDown != null)
                            {
                                (foundbuttonKnightLeftDown as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightDLeftDown:
                            var foundbuttonKnightDLeftDown = this.GetChildAtPoint(new Point(pos.getX() - ((2 * 75) * i), pos.getY() + ((75) * (i))));
                            if (foundbuttonKnightDLeftDown != null)
                            {
                                (foundbuttonKnightDLeftDown as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightDLeftUp:
                            var foundbuttonKnightDLeftUp = this.GetChildAtPoint(new Point(pos.getX() - ((2 * 75) * i), pos.getY() - ((75) * (i))));
                            if (foundbuttonKnightDLeftUp != null)
                            {
                                (foundbuttonKnightDLeftUp as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightDRightDown:
                            var foundbuttonKnightDRightDown = this.GetChildAtPoint(new Point(pos.getX() + ((2 * 75) * i), pos.getY() + ((75) * (i))));
                            if (foundbuttonKnightDRightDown != null)
                            {
                                (foundbuttonKnightDRightDown as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightDRightUp:
                            var foundbuttonKnightDRightUp = this.GetChildAtPoint(new Point(pos.getX() + ((2 * 75) * i), pos.getY() - ((75) * (i))));
                            if (foundbuttonKnightDRightUp != null)
                            {
                                (foundbuttonKnightDRightUp as Button).BackColor = Color.Green;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
}

insert pieces on board and then select a piece i want to show valid moves.

Because you are building all the buttons and delegate a click event btn_Click.

Inside the btn_Click event you are creating a NEW button which is not added to the form and use the btn_PieceClick event for the Click action on it. This button just exists as an object in the application. it is not displayed. You have to do the coloring part inside the btn_Click event.

But it shows an error! would test it and tell me...
how could i do that?
would copy the piece of code...?!
thanks so much...

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 Chess2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public class Position
        {
            private int X, Y;
            public Position(int x, int y)
            {
                X = x;
                Y = y;
            }
            public Position() { }
            public int getX()
            {
                return X;
            }
            public int getY()
            {
                return Y;
            }

        }
        public class Piece
        {
            public string name
            {
                get;
                set;
            }
            public Movement Moves
            {
                get;
                set;
            }

        }
        Piece Selectedpiece = new Piece();
        public enum Direction
        {
            Up,
            Down,
            Left,
            Right,
            UpRight,
            UpLeft,
            DownRight,
            DownLeft,
            KnightRightUp,
            KnightRightDown,
            KnightLeftUp,
            KnightLeftDown,
            KnightDRightUp,
            KnightDRightDown,
            KnightDLeftUp,
            KnightDLeftDown
        }
        public class Movement
        {
            public int Maxsteps
            {
                get;
                set;
            }
            public List<Direction> DirectionsList = new List<Direction>();
        }
        int p = 1;
        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            for (int i = 1; i <= 8; i++)
            {
                for (int j = 1; j <= 8; j++)
                {
                    Button btn = new Button();
                    btn.Width = 70;
                    btn.Height = 70;
                    btn.Left = (i) * (btn.Width + 5);
                    btn.Top = (j) * (btn.Width + 5);
                    btn.Text = i + "," + j;
                    Controls.Add(btn);
                    Position temp = new Position(btn.Left, btn.Top);
                    btn.Tag = temp;

                        btn.BackColor = Color.Gray;

                        btn.Click += new EventHandler(btn_Click);
                        p++;
                }


            }
        }
        int kingrem = 2;
        int queenrem = 2;
        int bishoprem = 4;
        int pawnrem = 16;
        int knightrem = 4;
        int rookrem = 4;
        int y = 0;
        private void btn_Click(object sender, EventArgs e)
        {
            y++;

            if (rbtnKing.Checked)
            {
                (sender as Button).Text = "King";
                (sender as Button).Enabled = false;
                (sender as Button).BackColor = Color.Azure;
                kingrem--;
                if (kingrem == 0)
                {
                    rbtnKing.Checked = false;
                    rbtnKing.Enabled = false;   
                }
                rtxtStatus.Text =rtxtStatus.Text+ "King Remaining :" + kingrem + Environment.NewLine;

            }
            if (rbtnBishop.Checked)
            {
                (sender as Button).Text = "bishop";
                (sender as Button).Enabled = false;
                (sender as Button).BackColor = Color.Azure;
                bishoprem--;
                if (bishoprem == 0)
                {
                    rbtnBishop.Enabled = false;
                    rbtnBishop.Checked = false;
                }
                rtxtStatus.Text = rtxtStatus.Text + "Bishop Remaining:" + bishoprem + Environment.NewLine;

            }
            if (rbtnQueen.Checked)
            {
                (sender as Button).Text = "queen";
                (sender as Button).Enabled = false;
                (sender as Button).BackColor = Color.Azure;
                queenrem--;
                if (queenrem == 0)
                {
                    rbtnQueen.Enabled = false;
                    rbtnQueen.Checked = false;
                }
                rtxtStatus.Text = rtxtStatus.Text + "Queen Remaining:" + queenrem + Environment.NewLine;
            }
            if (rbtnRook.Checked)
            {
                (sender as Button).Text = "rook";
                (sender as Button).Enabled = false;
                (sender as Button).BackColor = Color.Azure;
                rookrem--;
                if (rookrem == 0)
                {
                    rbtnRook.Enabled = false;
                    rbtnRook.Checked = false;
                }
                rtxtStatus.Text = rtxtStatus.Text + "Rook Remaining:" + rookrem + Environment.NewLine;
            }
            if (rbtnKnight.Checked)
            {
                (sender as Button).Text = "knight";
                (sender as Button).Enabled = false;
                (sender as Button).BackColor = Color.Azure;
                knightrem--;
                if (knightrem == 0)
                {
                    rbtnKnight.Enabled = false;
                    rbtnKnight.Checked = false;
                }
                rtxtStatus.Text = rtxtStatus.Text + "Knight Remaining:" + knightrem + Environment.NewLine;
            }
            if (rbtnPawn.Checked)
            {
                (sender as Button).Text = "pawn";
                (sender as Button).Enabled = false;
                (sender as Button).BackColor = Color.Azure;
                pawnrem--;
                if (pawnrem == 0)
                {
                    rbtnPawn.Enabled = false;
                    rbtnPawn.Checked = false;
                }
                rtxtStatus.Text = rtxtStatus.Text + "Pawn Remaining:" + pawnrem + Environment.NewLine;
            }
            if (kingrem == 0 && queenrem == 0 && bishoprem == 0 && knightrem == 0 && pawnrem == 0 && rookrem == 0)
            {
                foreach(var ctrl in this.Controls)
                {
                    if ((ctrl as Button) != null)
                    {
                        (ctrl as Button).Enabled = true;
                    }
                }
                Piece SelectedPiece = new Piece();
                if((sender as Button).Text=="King")
                {
                    Button btn = new Button();
                    Movement pieceMovement = new Movement();
                    pieceMovement.DirectionsList.Add(Direction.Down);
                    pieceMovement.DirectionsList.Add(Direction.Up);
                    pieceMovement.DirectionsList.Add(Direction.Left);
                    pieceMovement.DirectionsList.Add(Direction.Right);
                    pieceMovement.DirectionsList.Add(Direction.UpLeft);
                    pieceMovement.DirectionsList.Add(Direction.UpRight);
                    pieceMovement.DirectionsList.Add(Direction.DownLeft);
                    pieceMovement.DirectionsList.Add(Direction.DownRight);
                    pieceMovement.Maxsteps = 1;
                    SelectedPiece.Moves = pieceMovement;
                    btn.Click+=new EventHandler(btn_PieceClick);
                    SelectedPiece.name = "King";

                }

            }
            Position pos = new Position();
            pos = (Position)((Button)sender).Tag;
            for (int i = 1; i < Selectedpiece.Moves.Maxsteps; i++)
            {
                foreach (var direction in Selectedpiece.Moves.DirectionsList)
                {
                    switch (direction)
                    {
                        case Direction.Up:
                            var foundbuttonup = this.GetChildAtPoint(new Point(pos.getX(), pos.getY() - (75 * i)));
                            if (foundbuttonup != null || foundbuttonup.Enabled == true)
                            {
                                this.Text = "king up worked!";
                                (foundbuttonup as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.Down:
                            var foundButtonDown = this.GetChildAtPoint(new Point(pos.getX(), pos.getY() + (75 * i)));
                            if (foundButtonDown != null)
                            {
                                (foundButtonDown as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.Left:
                            var foundButtonLeft = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY()));
                            if (foundButtonLeft != null)
                            {
                                (foundButtonLeft as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.Right:
                            var foundButtonRight = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY()));
                            if (foundButtonRight != null)
                            {
                                (foundButtonRight as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.UpLeft:
                            var foundButtonUpLeft = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY() - (75 * i)));
                            if (foundButtonUpLeft != null)
                            {
                                (foundButtonUpLeft as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.UpRight:
                            var foundButtonUpRight = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY() - (75 * i)));
                            if (foundButtonUpRight != null)
                            {
                                (foundButtonUpRight as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.DownLeft:
                            var foundButtonDownLeft = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY() + (75 * i)));
                            if (foundButtonDownLeft != null)
                            {
                                (foundButtonDownLeft as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.DownRight:
                            var foundButtonDownRight = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY() + (75 * i)));
                            if (foundButtonDownRight != null)
                            {
                                (foundButtonDownRight as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightRightUp:
                            var foundbuttonknightrightup = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY() - ((75) * (2) * (i))));
                            if (foundbuttonknightrightup != null)
                            {
                                (foundbuttonknightrightup as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightRightDown:
                            var foundbuttonKnightRightDown = this.GetChildAtPoint(new Point(pos.getX() + (75 * i), pos.getY() + ((75) * (2) * (i))));
                            if (foundbuttonKnightRightDown != null)
                            {
                                (foundbuttonKnightRightDown as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightLeftUp:
                            var foundbuttonKnightLeftUp = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY() - ((75) * (2) * (i))));
                            if (foundbuttonKnightLeftUp != null)
                            {
                                (foundbuttonKnightLeftUp as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightLeftDown:
                            var foundbuttonKnightLeftDown = this.GetChildAtPoint(new Point(pos.getX() - (75 * i), pos.getY() + ((75) * (2) * (i))));
                            if (foundbuttonKnightLeftDown != null)
                            {
                                (foundbuttonKnightLeftDown as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightDLeftDown:
                            var foundbuttonKnightDLeftDown = this.GetChildAtPoint(new Point(pos.getX() - ((2 * 75) * i), pos.getY() + ((75) * (i))));
                            if (foundbuttonKnightDLeftDown != null)
                            {
                                (foundbuttonKnightDLeftDown as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightDLeftUp:
                            var foundbuttonKnightDLeftUp = this.GetChildAtPoint(new Point(pos.getX() - ((2 * 75) * i), pos.getY() - ((75) * (i))));
                            if (foundbuttonKnightDLeftUp != null)
                            {
                                (foundbuttonKnightDLeftUp as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightDRightDown:
                            var foundbuttonKnightDRightDown = this.GetChildAtPoint(new Point(pos.getX() + ((2 * 75) * i), pos.getY() + ((75) * (i))));
                            if (foundbuttonKnightDRightDown != null)
                            {
                                (foundbuttonKnightDRightDown as Button).BackColor = Color.Green;
                            }
                            break;
                        case Direction.KnightDRightUp:
                            var foundbuttonKnightDRightUp = this.GetChildAtPoint(new Point(pos.getX() + ((2 * 75) * i), pos.getY() - ((75) * (i))));
                            if (foundbuttonKnightDRightUp != null)
                            {
                                (foundbuttonKnightDRightUp as Button).BackColor = Color.Green;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            this.Text =Convert.ToString (y);
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            Piece SelectedPiece = new Piece();
            if (rbtnKing.Checked)
            {

            }
        }
        private void btn_PieceClick(object sender, EventArgs e)
        {

        }
    }
}

i click the select button shows this error:
Object reference not set to an instance of an object.

Just clicking the select button doesn't throw any exception for me. If you run the program from Visual Studio when it throws an uncaught exception the IDE jumps to the line of code where the exception occured. Check that line.

error is in this line :

for (int i = 1; i < Selectedpiece.Moves.Maxsteps; i++)

select a piece and then click on the board it shows the error how can i fix this?

It's because SelectedPiece is instantiated but it's properties are not. So SelectedPiece is just a new Piece qith nothing in it... no Moves, no name...

Look through your code... there is a point when you actually do that, just the conditions don't get you there all the time.

This article has been dead for over six months. Start a new discussion instead.