I'm making a chess game in C#, and I'm using 64 pictureBox
s to display the board. Now, I'm trying to figure out how to determine where the user clicks to allow the user to move pieces by clicking different tiles/pictureBoxes. I'm new to C#, but I'm trying to do it by detecting where the mouse is whenever it's clicked. I've been using:
void MainFormClick(object sender, EventArgs e){
Point mousePosition = pictureBox1.PointToClient(Cursor.Position); //pictureBox1 is in upper-left
int indexClicked = (mousePosition.X / tileSize) + (8 * (mousePosition.Y / tileSize));
if (indexClicked >= 0 && indexClicked <= 63){
//do code things?
}
this.Text = "Chess - " + indexClicked;
}
I assume this isn't working because the user isn't actually clicking on the form, but on the pictureBoxes. I'd rather not make 64 pictureBox*Click
methods, so is there some other way I should be doing this or method I should be using?
EDIT: I just looked at How to get control under mouse cursor. My code needs to be changed a little to better determine which pictureBox the mouse is over when it's clicked, but my real problem is that it doesn't appear that clicking on the pictureBoxes runs the above method. I'm looking for a way to run a block of code whenever the mouse is clicked, regardless of what control or part of the form it's clicked on.