Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a problem changing the backcolor of a dynamically created pixturebox when the picturebox is clicked. This code should change the backcolor of "currentpiece" but it doesn't or am i missing something? Please any help appreciated

What I have tried:

private void PieceClick(object sender, EventArgs e)
{
    RefressChessBoard();

    //Get the properties of the picturebox selected
    CurrentPiece = (PictureBox)sender;

    if (PieceMove)
    {
        foreach (PictureBox chessPiece in AllPieces)
	{
	    chessPiece.BackColor = Color.Transparent;
	    chessPiece.Visible = false;
	}
    }
    if (!PromotePawn)
    {
        //get the x coordinate  of the selected piece
	FromX = CurrentPiece.Left / 100;
	FromX = Convert.ToInt32(Math.Floor(FromX));

	//get the y coordinate of the selected piece
	FromY = CurrentPiece.Top / 100;
	FromY = Convert.ToInt32(Math.Floor(FromY));
 
	if (chessEngine.isPlayerPiece((byte)FromX, (byte)FromY))
	{
	     //Get the Piece that was selected
	     PieceSelected = chessEngine.CurrentBoard.GetPieceAtPosition((byte)FromX, (byte)FromY);

	     //Check if the player selected a pawn
	     if (CurrentPiece.Name == enPieceType.WhitePawn.ToString() || CurrentPiece.Name == enPieceType.BlackPawn.ToString())
             {
		  PossibleMoves = chessEngine.GetMoves((byte)FromX, (byte)FromY);
		  CurrentPiece.BackColor = Color.OliveDrab;
		  PieceMove = true;
	     }

	     DisplayAllPossibleMoves();
	}
}
Posted
Updated 27-Jun-19 20:04pm
v2

That's going to depend on the rest of your application: and we don't have access to that. It's also probably going to need your app to be running at the time in order to work out what is going on, and we certainly don't have access to that!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
ZamianZ 28-Jun-19 1:21am    
I have tried debugging and i put a watch on all of the properties of the "CurrentPiece" and from what i can see it does change it but it doesn't want to change that on the form which is very confusing to me because it should I'm not even touching it thereafter.
OriginalGriff 28-Jun-19 1:33am    
So start by adding a separate "piece" picture box in the designer, add the handler to that and do nothing else to it - add no image, no nothing.
Then click and see what happens. Do you get the right "piece" in the debugger? does the background change? Follow the code through the whole method and see what happens.
We can't do that for you!
Assuming this is a WinForm app:

1. the extent to which the 'BackColor of a 'PictureBox will be visible will depend on:

a. the degree of transparency of the current 'PictureBox 'Image

and:

b. the values of the 'Padding property that offset the 'Image from the display area.

So, are your images .png files with some degree of transparency, or, are you manipulating images in some other format to make them transparent ?

Do you want the 'BackColor to show only around the border area made visible by the 'Padding settings ?
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900