I took this error in line I commented, what's the problem?
private void pictureBox34_Click(object sender, EventArgs e)
{
if (pictureBox34.Image == chess9.Properties.Resources.siyahsah2)
{
f();
}
}
public void picarray()
{
pic[0, 0] = pictureBox54;
pic[0, 1] = pictureBox64;
pic[0, 2] = pictureBox48;
pic[0, 3] = pictureBox42;
pic[0, 4] = pictureBox34;
pic[0, 5] = pictureBox26;
pic[0, 6] = pictureBox18;
pic[0, 7] = pictureBox8;
pic[1, 0] = pictureBox1;
开发者_运维百科 pic[1, 1] = pictureBox2;
pic[1, 2] = pictureBox3;
pic[1, 3] = pictureBox4;
...
}
public void f()
{
// int i = 0, j = 0;
int x = 3;
int y = 3;
for (int i = 1; i < x; i++)
{
for (int j = 1; j < y; j++)
{
pic[i, j] = new PictureBox();
// pic[i, j] = pic[i + 1, j + 1];
pic[i, j].Image = chess9.Properties.Resources.siyahsah2;
}
}
}
I happened to read your previous question, so if I understand correctly, this is an implementation for a chess game.
Instead of trying to fix the exception in this code, I would suggest that you make some changes to your design.
The main change: separate the representation of your game-board status, from the UI layer. If I understand correctly, you are using an array of PictureBox objects to represent the pieces on the board. You would probably be better off if you write classes that represent the board, pieces and all their actions, and then write seperate code that can display these classes in a window, and take commands from the user through UI.
When you are implementing piece movements and board updates through manipulation of UI objects, you are bound to make more errors and spend much more time debugging.
I hope that helps...
Make sure the file indicated actually exists. Also, preferably change
/
to\\
(or\
in a@"..."
string) in the file path, if you're running this on a Windows system. (Though that shouldn't be the cause of your issue.)... = Image.FromFile( @"pic\siyahsah2.jpg" );
Make sure the indices
i
andj
are correct. Yourfor
loops indicate that they will be any combination of1
and2
.
Besides, inside your for
loop:
pic[i, j] = new PictureBox(); // <-- will get overwritten by (*)
pic[i, j] = pic[i + 1, j + 1]; // <-- will get overwritten by (*)
pic[i, j] = new PictureBox(); // <-- (*)
pic[i, j].Image = Image.FromFile(...);
That is, you can delete the first two lines.
I don't know why you're trying to do it so complicated, but assuming the knight is at picturebox34
or in other words at pic[0, 4]
and you want to move it to the right and up and thus end up at pic[2, 3]
then it's nothing more than doing this
// Move knight image from 0,4 to 2, 3
pic[2, 3].Image = pic[0, 4].Image;
// Make old knight position empty
pic[0, 4].Image = null;
I have no idea why're you doing the for loops and such..
精彩评论